How to avoid reading letters "NA" as missing (or NA)?

Viewed 48

I have a large file (csv) of library records that include Library of Congress classification call numbers, from which I have extracted the letters. The architecture subclass is NA. So a record classified as "NA 1053 .B69 P4 1994" has "NA" extracted in different column. However, when reading this file into R, these NA letters are read as NA, or missing values. Is there any way to avoid this, maybe by designating what should be considered "missing," and how those are coded?

1 Answers

Depending on what function you are using to read the csv, you can override the default behaviours to interpret "NA" as NA. Most delimited data reading functions have optional arguments to control what is considered missing.

  • With read.csv, the argument is na.strings, defaulting to "NA".

  • With readr::read_csv, the argument is na, defaulting to c("", "NA").

  • With data.table::fread, the argument is also called na.strings, defaulting to "NA" but also controlled by a package option. More details in the NEWS.

So in your example, you would write something like readr::read_csv(file = "path/to/file", na = "") so "NA" in the file is not interpreted as NA. Of course, it usually only makes sense to do this in a situation like yours where "NA" has meaning and does not represent a missing value.

Related