Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: what is NA_character_?

Tags:

r

>dput(dummy)
c(NA, "MGM", NA, "M", NA)

>dummy
[1] NA    "MGM" NA    "M"   NA 

>ifelse(dummy == "NA", 0, 1)
NA  1 NA  1 NA

I have a character vector, and I want to replace the NA values with 0 and everything else as 1. However, I have no idea what exactly these NA characters are? As you can see above they're not "NA". When I tried dput(dummy[1]) I get NA_character_. What can I do to get the ifelse statement to recognize those NAs?

like image 468
Adrian Avatar asked May 23 '16 21:05

Adrian


People also ask

What is the Na character in R?

In R, missing values are represented by the symbol NA (not available). Impossible values (e.g., dividing by zero) are represented by the symbol NaN (not a number). Unlike SAS, R uses the same symbol for character and numeric data.

What does Na_real mean in R?

There are actually many different flavours of NA values in R: NA is a logical. NA_character_ is characters. NA_integer_ is integer values. NA_real_ is doubles (values with decimal points)

How do I check if a value is na in R?

To check which value in NA in an R data frame, we can use apply function along with is.na function. This will return the data frame in logical form with TRUE and FALSE.

What does as character do in R?

character() function in R converts a numeric object to a string data type or a character object. If the collection is passed to it as an object, it converts all the elements of the collection to a character or string type.


1 Answers

In R, nothing is ever equal to NA (how could anything be == to NA when we don't know what it is?) but it is possible to be equal to "NA" (the character value with two letters as in the abbreviation for North America. NA (no quotes)is a special missing value placeholder and actually comes in different flavors, one for each atomic class.

 ?NA   # where you can read about the other flavors of NA
 NA_character_
#[1] NA

As C_Z_ notes you can and should use is.na() to detect these special (non)-values. Your ifelse test would have returned a 0 in any position where the character vector had a value of "NA", but a test for equality using "==" to NA will always return NA.

> dummy == NA
[1] NA NA NA NA NA

These might have been less puzzling:

> ifelse( is.na(dummy), 0, 1)
[1] 0 1 0 1 0
> 0+!is.na(dummy)   # using 0+... to coerce logical to numeric
[1] 0 1 0 1 0
like image 94
IRTFM Avatar answered Nov 02 '22 09:11

IRTFM