Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <NA> and NA?

Tags:

r

missing-data

na

I have a factor named SMOKE with levels "Y" and "N". Missing values were replaced with NA (from the initial level "NULL"). However when I view the factor I get something like this:

head(SMOKE)
# N N <NA> Y Y N
# Levels: Y N

Why is R displaying NA as <NA>? And is there a difference?

like image 235
oort Avatar asked Apr 27 '13 15:04

oort


People also ask

Is there any difference Na and Na+?

Because of equal number of positive charge and negative charge they cancel each other . that's why normal sodium atom (Na) is electrically neutral. But Na+ is positive ion of sodium. during chemical reaction sodium atom (Na) loses his one electron present last orbit.

Why is Na smaller than Na?

Yes Na+ is smaller than Na because Na+ is formed when an electron is lost from the Na atom,Thus the effective nuclear charge increases bcz the number of protons exceeds the number of electrons . This results in bringing the valence shell a little closer to the nucleus because of a very strong nuclear pull.

Why is Na bigger than Na?

The Na+ ion is significantly smaller than the neutral Na atom because the 3s1 electron has been removed to give a closed shell with n = 2. The Na− ion is larger than the parent Na atom because the additional electron produces a 3s2 valence electron configuration, while the nuclear charge remains the same.

What is the difference between Na and Na * in terms of electron?

Whereas, in Na⁺, there is one less electron compared to the normal sodium atom. Na is an atom with 11 electrons, and when Na loses one electron, Na+ is created as an ion.


2 Answers

When you are dealing with factors, when the NA is wrapped in angled brackets ( <NA> ), that indicates thtat it is in fact NA.

When it is NA without brackets, then it is not NA, but rather a proper factor whose label is "NA"

# Note a 'real' NA and a string with the word "NA"
x <- factor(c("hello", NA, "world", "NA"))

x
[1] hello <NA>  world NA   
Levels: hello NA world      <~~ The string appears as a level, the actual NA does not. 

as.numeric(x)              
[1]  1 NA  3  2            <~~ The string has a numeric value (here, 2, alphabetically)
                               The NA's numeric value is just NA

Edit to answer @Arun's question:

R is simply trying to distinguish between a string whose value are the two letters "NA" and an actual missing value, NA Thus the difference you see when displaying df versus df$y. Example:

df <- data.frame(x=1:4, y=c("a", NA_character_, "c", "NA"), stringsAsFactors=FALSE)

Note the two different styles of NA:

> df
  x    y
1 1    a
2 2 <NA>
3 3    c
4 4   NA

However, if we look at just 'df$y'

[1] "a"  NA   "c"  "NA"

But, if we remove the quotation marks (similar to what we see when printing a data.frame to the console):

print(df$y, quote=FALSE)
[1] a    <NA> c    NA  

And thus, we once again have the distinction of NA via the angled brackets.

like image 81
Ricardo Saporta Avatar answered Sep 24 '22 15:09

Ricardo Saporta


It is just the way that R displays NA in a factor:

> as.factor(NA)
[1] <NA>
Levels: 
> 
> f <- factor(c(1:3, NA))
> levels(f)
[1] "1" "2" "3"
> f
[1] 1    2    3    <NA>
Levels: 1 2 3
> is.na(f)
[1] FALSE FALSE FALSE  TRUE

One presumes this is a means by which one would differentiate between NA and "NA" in the way a factor is printed as it prints without the quotes, even for character labels/levels:

> f2 <- factor(c("NA",NA))
> f2
[1] NA   <NA>
Levels: NA
> is.na(f2)
[1] FALSE  TRUE
like image 43
Gavin Simpson Avatar answered Sep 24 '22 15:09

Gavin Simpson