user1938803
user1938803

Reputation: 189

R + NA's are not included

Consider this file:

"a","b"

"a","v","d"

Now, if I read this in with:

d <- read.csv("tmp.txt", header=0, fill=TRUE);

then d becomes

a,b

a,b,c

whereas I want it to be

a,b,NA

a,b,c

so I can check for NA (since R doesnt have a is.empty operation). My question is: Why on earth doesnt read.csv just do this? I've tried every single combination and it doesnt work. However, if I exchange the rows and remove the header=0 option then it does work, but the first row becomes the header. So irritating.

Upvotes: 1

Views: 72

Answers (1)

tkmckenzie
tkmckenzie

Reputation: 1363

You need to specify that empty strings should be interpreted as NA:

> d <- read.csv("tmp.txt", header = F, na.strings = c("", "NA"))
> d
  V1 V2   V3
1  a  b <NA>
2  a  v    d

Cheers!

Upvotes: 2

Related Questions