Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace <NA> with NA

Tags:

replace

r

na

I have a data frame containing entries; It appears that these values are not treated as NA since is.na returns FALSE. I would like to convert these values to NA but could not find the way.

like image 348
user34771 Avatar asked Oct 06 '14 16:10

user34771


2 Answers

Use dfr[dfr=="<NA>"]=NA where dfr is your dataframe.

For example:

> dfr<-data.frame(A=c(1,2,"<NA>",3),B=c("a","b","c","d"))

> dfr
     A  B
1    1  a
2    2  b
3 <NA>  c
4    3  d

> is.na(dfr)
         A     B
[1,] FALSE FALSE
[2,] FALSE FALSE
[3,] FALSE FALSE
[4,] FALSE FALSE

> dfr[dfr=="<NA>"] = NA                 **key step**

> is.na(dfr)
         A     B
[1,] FALSE FALSE
[2,] FALSE FALSE
[3,]  TRUE FALSE
[4,] FALSE FALSE
like image 52
Ujjwal Avatar answered Oct 18 '22 15:10

Ujjwal


The two classes where this is likely to be an issue are character and factor. This should loop over a dtaframe and convert the "NA" values into true <NA>'s but just for those two classes:

make.true.NA <- function(x) if(is.character(x)||is.factor(x)){
                                  is.na(x) <- x=="NA"; x} else {
                                  x}
df[] <- lapply(df, make.true.NA)

(Untested in the absence of a data example.) The use of the form: df_name[] will attempt to retain the structure of the original dataframe which would otherwise lose its class attribute. I see that ujjwal thinks your spelling of NA has flanking "<>" characters so you might try this functions as more general:

make.true.NA <- function(x) if(is.character(x)||is.factor(x)){
                                  is.na(x) <- x %in% c("NA", "<NA>"); x} else {
                                  x}
like image 31
IRTFM Avatar answered Oct 18 '22 15:10

IRTFM