简体   繁体   中英

How can I get R to read these cells as blank instead of NA?

I have a csv file that looks like this:

column1,column2,column3
data,,
more data,data,

I read it using

read.csv("file.csv")

And I get

> read.csv("file.csv")
    column1 column2 column3
1      data              NA
2 more data    data      NA

Is it possible to get R to read the data in column3 as empty cells, just as it does with the empty cell in column2?

From help('read.csv') , section Arguments , my emphasis:

na.strings
a character vector of strings which are to be interpreted as NA values. Blank fields are also considered to be missing values in logical, integer, numeric and complex fields . Note that the test happens after white space is stripped from the input, so na.strings values may need their own white space stripped in advance.

The above highlighted part does not refer to "character" class. So define the column of interest to be of class "character" .

In the question's case, all 3 columns are of the same class and since argument colClasses recycles its value, the following is enough.

read.csv("file.csv", colClasses = "character")
#    column1 column2 column3
#1      data                
#2 more data    data        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM