R语言中na.omit函数
1、应用于向量
> a <- c("d", "e", NA, "k", NA, "m") > a [1] "d" "e" NA "k" NA "m" > as.character(na.omit(a)) ## 清除向量中的空值 [1] "d" "e" "k" "m" > na.omit(a)[1:length(na.omit(a))] ## 清除向量中的空值 [1] "d" "e" "k" "m"
2、应用于数据框
> a <- c("d", "e", NA, "k", NA, "m") > b <- 1:6 > c <- c(2, NA, 3, 7, 8, 9) > d <- letters[1:6] > e <- data.frame(a, b, c, d) > e a b c d 1 d 1 2 a 2 e 2 NA b 3 <NA> 3 3 c 4 k 4 7 d 5 <NA> 5 8 e 6 m 6 9 f > na.omit(e) ## 清除所有含有空值的行 a b c d 1 d 1 2 a 4 k 4 7 d 6 m 6 9 f