缺失值用NA表示,但是如果我们将其读取为“ NA”,则它将成为因子变量的级别。如果我们认为向量是数字向量,并且在该向量中有“ NA”,那么它将不是数字向量。另一方面,如果我们有一个带有NA的向量,那么它将是一个数值向量。
x1<-c("A","B","NA","D",NA,"F") x1 [1] "A" "B" "NA" "D" NA "F" x1<-factor(c("A","B","NA","D",NA,"F")) x1 [1] A B NA D <NA> F Levels: A B D F NA x2<-factor(c("A","B","D",NA,"F")) x2 [1] A B D <NA> F Levels: A B D F x3<-factor(c("Sweet","Bitter","Salty",NA)) x3 [1] Sweet Bitter Salty <NA> Levels: Bitter Salty Sweet x4<-factor(c("Sweet","Bitter","Salty","NA")) x4 [1] Sweet Bitter Salty NA Levels: Bitter NA Salty Sweet x5<-factor(c("Sweet","Bitter","Salty","NA",NA)) x5 [1] Sweet Bitter Salty NA <NA> Levels: Bitter NA Salty Sweet x6<-factor(c("India","China",NA,"USA","UK")) x6 [1] India China <NA> USA UK Levels: China India UK USA x7<-factor(c("India","China",NA,"USA","UK","NA")) x7 [1] India China <NA> USA UK NA Levels: China India NA UK USA x8<-factor(c("Level1","Level2","Level3","Level4","NA","Level6","Level7")) x8 [1] Level1 Level2 Level3 Level4 NA Level6 Level7 Levels: Level1 Level2 Level3 Level4 Level6 Level7 NA x9<-factor(c("Level1","Level2","Level3","Level4","NA","Level6","Level7",NA)) x9 [1] Level1 Level2 Level3 Level4 NA Level6 Level7 <NA> Levels: Level1 Level2 Level3 Level4 Level6 Level7 NA x10<-factor(c("Level1","Level2","Level3","Level4","Level6","Level7",NA)) x10 [1] Level1 Level2 Level3 Level4 Level6 Level7 <NA> Levels: Level1 Level2 Level3 Level4 Level6 Level7 x11<-c(1,2,3,4,"NA") x11 [1] "1" "2" "3" "4" "NA" is.numeric(x11) [1] FALSE x12<-c(1,2,3,4,NA) x12 [1] 1 2 3 4 NA is.numeric(x12) [1] TRUE