suppressMessages(library(dplyr)); suppressMessages(library(purrr)) # now go ahead and load them
data_gapminder <- read.csv(file.path("..", "..", "data", "gapminder_nas.csv"))
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
length(x_na) # 1 -- one slot, value unknown
## [1] 1
typeof(x_null) # "NULL"
## [1] "NULL"
typeof(x_na) # "logical" -- NA has a type!
## [1] "logical"
# The difference shows up inside vectors
c(1, NULL, 3) # 1 3 -- NULL just vanishes
## [1] 1 3
c(1, NA, 3) # 1 NA 3 -- NA keeps its place
## [1] 1 NA 3
# Testing for each
is.null(x_null) # TRUE
## [1] TRUE
is.na(x_na) # TRUE
## [1] TRUE
is.na(x_null) # logical(0) + a warning -- the wrong test for NULL
## logical(0)
# NA is "contagious": unknown in, unknown out
NA + 1 # NA
## [1] NA
NA == NA # NA, not TRUE! Use is.na() instead
## [1] NA
mean(c(1, NA, 3)) # NA
## [1] NA
mean(c(1, NA, 3), na.rm = TRUE) # 2
## [1] 2
# tracemem() needs an actual object to trace
# tracemem(x_null) # Error: cannot trace NULL
na_values <- c("NA", "NULL", "", " ")
data_gapminder <- read.csv(file.path("..", "..", "data", "gapminder_nas.csv"), na.strings = na_values)
# is.na()
complete_gapminder <- complete.cases(data_gapminder)
complete_gapminder <- data_gapminder[which(complete_gapminder),]
#typeof(complete_gapminder)
mean(data_gapminder[which(data_gapminder$continent == "Americas"),]$lifeExp, na.rm = TRUE)
## [1] 64.32363
mean(complete_gapminder[which(complete_gapminder$continent == "Americas"),]$lifeExp)
## [1] 61.70087
# Compare sample size before and after complete.cases
nrow(data_gapminder)
## [1] 1704
sum(complete.cases(data_gapminder))
## [1] 853
# Simple example of mean imputation
data_imputed <- data_gapminder
data_imputed$lifeExp[is.na(data_imputed$lifeExp)] <-
mean(data_imputed$lifeExp, na.rm = TRUE)
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
unique(data_gapminder$continent)
## [1] "Asia" "Europe" "Africa" "Americas" "Oceania"
# on a vector
head(duplicated(data_gapminder$continent))
## [1] FALSE TRUE TRUE TRUE TRUE TRUE
# there are 5 unique continents and 1699 duplicates
sum(duplicated(data_gapminder$continent))
## [1] 1699
# on a data frame
head(duplicated(data_gapminder))
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
# there are no duplicate values
sum(duplicated(data_gapminder))
## [1] 0
length(data_gapminder$country)
## [1] 1704
length(data_gapminder) # will return ncol, but that's confusing
## [1] 6
ncol(data_gapminder)
## [1] 6
nrow(data_gapminder)
## [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 datadata_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 programmingdata_gapminder %>%
split(.$continent) %>%
map(~ mean(.x$lifeExp, na.rm = TRUE))
## $Africa
## [1] 48.62973
##
## $Americas
## [1] 64.32363
##
## $Asia
## [1] 59.62585
##
## $Europe
## [1] 71.74504
##
## $Oceania
## [1] 74.47852

hist(data_gapminder$gdpPercap)
:::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. :::
hist(data_gapminder$gdpPercap, breaks = 30)
:::notes And you can increase the ‘buckets’ with the breaks argument
:::
dens <- density(data_gapminder$lifeExp, na.rm = TRUE)
plot(dens)
:::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

boxplot(data_gapminder$lifeExp ~ data_gapminder$continent)
:::notes boxplot() allows you to facet one variable by another using a
tilde ~ to define the relationship :::
plot(x = data_gapminder$gdpPercap, y = data_gapminder$lifeExp)

# log transform gdp per capita
plot(x = log(data_gapminder$gdpPercap), y = data_gapminder$lifeExp)
