What is the difference between NA and in R?


The missing values are represented by NA but if we read them as "NA" then it becomes a level of a factor variable. If we believe that a vector is numeric and we have an "NA" in that vector then it will not be a numeric vector. On the other hand, if we have a vector with NA then it will be a numeric vector.

Examples

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

Updated on: 24-Aug-2020

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements