is.na Function in R (3 Examples)
This article shows how to use the is.na() function in the R programming language.
Example Data
x <- c(4, 1, NA, 7, 2, NA, 5) # Create example vector |
x <- c(4, 1, NA, 7, 2, NA, 5) # Create example vector
Return Logical Vector with is.na Function
is.na(x) # Return logical vector # FALSE FALSE TRUE FALSE FALSE TRUE FALSE |
is.na(x) # Return logical vector # FALSE FALSE TRUE FALSE FALSE TRUE FALSE
Remove NA Values from Vector
x[!is.na(x)] # Remove NA values # 4 1 7 2 5 |
x[!is.na(x)] # Remove NA values # 4 1 7 2 5
Replace NA with 0 in R
x[is.na(x)] <- 0 # Replace NA with 0 x # 4 1 0 7 2 0 5 |
x[is.na(x)] <- 0 # Replace NA with 0 x # 4 1 0 7 2 0 5