Section 32 Missing values: Operation


Many functions will return NA if a numeric vector includes NA

Most functions can include na.rm=TRUE for removing NA values prior to applying the function.


32.1 Example: Numeric vector

Many functions will return NA if a numeric vector includes NA

Most functions can include na.rm=TRUE for removing NA values prior to applying the function.

x <- c(1, 2, NA, 10, 3)
sum(x)
mean(x)
sum(x, na.rm=TRUE)
mean(x, na.rm=TRUE)
mean(na.omit(x))
sqrt(x)

y <- c(1, 2, NaN, NA, 4)
sum(y)
mean(y)
sum(y, na.rm=TRUE)
mean(y, na.rm=TRUE)


x
y
x + y
x - y
x * y
x / y

32.2 Example: Logical vector

x <- c(T, F, NA, TRUE, FALSE)
sum(x)
sum(x, na.rm=TRUE)

y <- c(T, F, NaN, NA, TRUE, FALSE)
sum(y)
sum(y, na.rm=TRUE)