Run data_gapminder in your console
NA stands for “not available”, a special
boolean for missing data within an object
NA_integer_, NA_real_,
NA_complex_ and NA_character_
NULL used for empty objects (points to the same
singleton place in memory), think of it like an object purgatory. It
exists and…it doesn’t.
Analogous (but perhaps not equivalent to) SASS
SYSMIS
RNA |
NULL |
|
|---|---|---|
| Meaning | A value exists but is unknown | No value at all |
| Length | 1 | 0 |
| Type | logical by default (also NA_integer_,
NA_real_, NA_character_) |
"NULL" |
| In a vector | Keeps its slot: c(1, NA, 3) → 1 NA 3 |
Disappears: c(1, NULL, 3) → 1 3 |
| In calculations | Spreads: NA + 1 → NA |
Returns empty: NULL + 1 → numeric(0) |
| How many exist | Many (each is its own value in memory) | One shared object per session |
tracemem() |
Works | Error: cannot trace NULL |
| Test with | is.na() |
is.null() |
| Typical source | Skipped survey answer, failed conversion | Missing list element, function returning nothing |
R handles ‘missingness’# NULL = "nothing here"; NA = "something here, but we don't know what"
x_null <- NULL
x_na <- NA
length(x_null) # 0 -- no object, no slot## [1] 0
## [1] 1
## [1] "NULL"
## [1] "logical"
## [1] 1 3
## [1] 1 NA 3
## [1] TRUE
## [1] TRUE
## logical(0)
## [1] NA
## [1] NA
## [1] NA
## [1] 2
## [1] 64.32363
## [1] 61.70087
## [1] 1704
## [1] 853
complete_gapminder <- complete.cases(data_gapminder) # for later use
# first few index numbers of complete cases
head(which(complete_gapminder))## [1] 2 3 4 5 8 12
## [1] "Asia" "Europe" "Africa" "Americas" "Oceania"
## [1] FALSE TRUE TRUE TRUE TRUE TRUE
## [1] 1699
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
## [1] 0
## [1] 1704
## [1] 6
## [1] 6
## [1] 1704
range(data_gapminder$year, na.rm = TRUE)
mean(data_gapminder$lifeExp, na.rm = TRUE)
median(data_gapminder$gdpPercap, na.rm = TRUE)
min(data_gapminder$year, na.rm = TRUE)
max(data_gapminder$year, na.rm = TRUE)
quantile(data_gapminder$gdpPercap, na.rm = TRUE)
var(data_gapminder$lifeExp, na.rm = TRUE)
sd(data_gapminder$lifeExp, na.rm = TRUE)# kable(summary(data_gapminder$gdpPercap)) # could also summarise just one column
summary(data_gapminder)## country continent year lifeExp pop gdpPercap
## Length :1704 Length :1704 Min. :1952 Min. :23.60 Min. :6.001e+04 Min. : 241.2
## N.unique : 142 N.unique : 5 1st Qu.:1962 1st Qu.:47.83 1st Qu.:2.736e+06 1st Qu.: 1147.4
## N.blank : 0 N.blank : 0 Median :1977 Median :59.63 Median :6.703e+06 Median : 3242.5
## Min.nchar: 4 Min.nchar: 4 Mean :1979 Mean :58.90 Mean :2.835e+07 Mean : 6839.5
## Max.nchar: 24 Max.nchar: 8 3rd Qu.:1992 3rd Qu.:70.60 3rd Qu.:1.836e+07 3rd Qu.: 8533.1
## Max. :2007 Max. :82.60 Max. :1.319e+09 Max. :113523.1
## NAs :217 NAs :192 NAs :223 NAs :219
## country continent year lifeExp pop gdpPercap
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60 Min. :6.001e+04 Min. : 241.2
## Albania : 12 Americas:300 1st Qu.:1962 1st Qu.:47.83 1st Qu.:2.736e+06 1st Qu.: 1147.4
## Algeria : 12 Asia :396 Median :1977 Median :59.63 Median :6.703e+06 Median : 3242.5
## Angola : 12 Europe :360 Mean :1979 Mean :58.90 Mean :2.835e+07 Mean : 6839.5
## Argentina : 12 Oceania : 24 3rd Qu.:1992 3rd Qu.:70.60 3rd Qu.:1.836e+07 3rd Qu.: 8533.1
## Australia : 12 Max. :2007 Max. :82.60 Max. :1.319e+09 Max. :113523.1
## (Other) :1632 NAs :217 NAs :192 NAs :223 NAs :219
DPLYR: A better way to do datamean_life | sd_life | min_life | max_life |
|---|---|---|---|
58.9021 | 12.85151 | 23.599 | 82.603 |
data_gapminder %>%
summarise(
mean_life = mean(lifeExp, na.rm = TRUE),
sd_life = sd(lifeExp, na.rm = TRUE),
min_life = min(lifeExp, na.rm = TRUE),
max_life = max(lifeExp, na.rm = TRUE)
) %>% flextable() %>% flextable::theme_vanilla() %>%
flextable::autofit() %>% # adjusts columns to content
flextable::set_table_properties( # ensures full-width fit
width = 1,
layout = "autofit"
)DPLYR: groupsdata_gapminder %>%
# `group_by` this automatically groups things by class for you very powerful
group_by(continent) %>%
summarise(
mean_life = mean(lifeExp, na.rm = TRUE),
n = n()
)## # A tibble: 5 × 3
## continent mean_life n
## <fct> <dbl> <int>
## 1 Africa 48.6 624
## 2 Americas 64.3 300
## 3 Asia 59.6 396
## 4 Europe 71.7 360
## 5 Oceania 74.5 24
PURRRfectly: functional programming## $Africa
## [1] 48.62973
##
## $Americas
## [1] 64.32363
##
## $Asia
## [1] 59.62585
##
## $Europe
## [1] 71.74504
##
## $Oceania
## [1] 74.47852

:::notes You can plot a basic histogram with hist()
Plotting for presentation will be covered in greater detail in a future
session. Basic plotting for distribution analysis is relatively straight
forward in base R. :::
:::notes And you can increase the ‘buckets’ with the breaks
argument :::
:::notes A density plot can be generated with plot(), but
requires the extra step of computing the density of the data with the
density() function first ::: # Distribution of Two
Variables

:::notes boxplot() allows you to facet one variable by
another using a tilde ~ to define the relationship :::

