> a <- 2:12> b <- seq (2,23,2) > c <- c (1:11) ^3 > d <- c (5:8, 30:36) > df <- data.frame (a,b,c,d) > df$a[ df$a==8] <- na > df$b[df$b==8] <- na > df$c[df$c==8] <- na > df$d[df$d==8] <- na > df$d[df$d==32] <- na > df a b c d1 2 2 1 52 3 4 NA 63 4 6 27 74 5 NA 64 NA5 6 10 125 306 7 12 216 317 na 14 343 na8 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36// Remove the corresponding line > bad.d <- is.na (DF$D) based on the fourth column, which is d na > bad.d [1] false false false true false false true false false false false> df[!bad.d,] a b c d1 2 2 1 52 3 4 na 63 4 6 27 75 6 10 125 306 7 12 216 318 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36// removes the corresponding row > df[complete.cases based on the na of the second and third columns (DF [, 2:3]),] a b c d1 2 2 1 53 4 6 27 75 6 10 125 306 7 12 216 317 na 14 343 NA8 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36// according to the second column and the NA in column fourth, Remove the corresponding line > df[complete.cases (Df[,c (2,4)),] a b C d1 2 2 1 52 3 4 NA 63 4 6 27 75 6 10 125 306 7 12 216 318 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36// removes the corresponding row > df[complete.cases (DF), based on the Na of all columns,] a b c d1 2 2 1 53 4 6 27 75 6 10 125 306 7 12 216 318 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36> // This effect is the same as the above df[complete.cases (DF),] > na.omit (DF) a b c d1 2 2 1 53 4 6 27 75 6 10 125 306 7 12 216 318 9 16 512 339 10 18 729 3410 11 20 1000 3511 12 22 1331 36// calculates the average of a column, removes the NA value > mean (df$d, na.rm=true)
This article is from the "R Language Learning history" blog, please be sure to keep this source http://matrix6ro.blog.51cto.com/1746429/1791239
R language Removal missing value na.rm