Reputation: 682
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?
Upvotes: 0
Views: 1748
Reputation: 76663
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
Upvotes: 3