What's the best way to replace missing values with NA when reading in a .csv?

后端 未结 2 1514
Happy的楠姐
Happy的楠姐 2020-12-24 09:51

I have a .csv dataset with many missing values, and I\'d like R to recognize them all the same way (the \"correct\" way) when I read the table in. I\'ve been using:

相关标签:
2条回答
  • 2020-12-24 09:57

    You can also use the more flexible readr package, whose equivalent function and argument are read_csv() and na.

    library(readr)
    read_csv("file.csv", na = c(".", ".."))
    
    0 讨论(0)
  • 2020-12-24 10:14

    The <NA> vs NA just means that some of your columns are character and some are numeric, that's all. Absolutely nothing is wrong with that.

    As Ben mentioned above, if some of your missing values in the csv are represented by a single period, ., then you can specify a vector of values that should be treated as NAs via:

    na.strings=c("",".","NA")
    

    as an argument to read.csv.

    0 讨论(0)
提交回复
热议问题