Part 10 Missing values

NA (Not Available) is a recognized element in R.

  • Finding missing values in a vector
# Create vector
x <- c(4, 2, 7, NA)

# Find missing values in vector:

# Remove missing values
x[ ! ]
  • Some functions can deal with NAs, either by default, or with specific arguments:
x <- c(4, 2, 7, NA)

# default arguments

# set na.rm=TRUE
mean(x, na.rm=TRUE)
  • In a matrix or a data frame, keep only rows where there are no NA values:
# Create matrix with some NA values
mydata <- matrix(c(1:10, NA, 12:2, NA, 15:20, NA), ncol=3)

# Keep only rows without NAs
mydata[complete.cases(mydata), ]
# or

Check this R blogger post on missing/null values