Home > Blockchain >  Change default NA from logical to character
Change default NA from logical to character

Time:10-18

Is there a way to change the default NA (missing) from logical to character (NA_character_) for an entire R session?

For example, if you load a CSV where one column is empty, it will be filled with NA, and the class of that NA will be logical. For this question, we want a way to ensure that it will always be NA_character_. Not to be confused with the literal string "NA".

More examples:


> class(NA)
"logical" # No!
> class(NA_character_)
"character" # Yes! but for NA!

CodePudding user response:

Not sure if I understand but you could specify na.strings argument.

Ex:

df <- read.table(text='
      a    b  c d  e
   1 56 43.0 12 1 NA
   2 23   NA  7 2 45
   3 15 90.7 10 3  2
   4 10 30.5  2 4 NA', na.strings="", as.is=T)

And:

> class(df$b)
[1] "character"
> 

CodePudding user response:

As far as I can see, the answer is no:

from the documentation of NA

Details
The NA of character type is distinct from the string "NA". Programmers who need to specify an explicit missing string should use NA_character_ (rather than "NA") or set elements to NA using is.na<-.

I browsed thought the list of input parameters to the 'options' function and nothing seem to apply here.

I think the best way and safest is to explicitly define the NAs as they are likely to be encountered. For the csv-case in your example I would recommend the readr package where the 'col_types' is used to define classes.

  •  Tags:  
  • r
  • Related