isilya
isilya

Reputation: 61

R is adding 72 rows of "NA" entries in a data frame

So as the title says, R is randomly adding rows of "NA" entries in a data frame. This only happens when I index into it. So a line like:

data[data$subject_code==1,]

will produce all the data I want, but with 72 rows at the end that all look like this:

NA.8        NA   NA             NA

Why is this happening?? It doesn't make any sense...there is not a single entry that has the value "NA" in the entire data frame.

Upvotes: 0

Views: 72

Answers (1)

agstudy
agstudy

Reputation: 121568

No you have missing values in your data. see is.na or complete.cases. I guess you get an error because you try to check missing values using =="NA".

dat <- data.frame(x=cbind(NA,NA,NA,NA))
dat
  x.1 x.2 x.3 x.4
1  NA  NA  NA  NA
## this reproduce the same output
 dat[dat$x.1==1,]
   x.1 x.2 x.3 x.4
NA  NA  NA  NA  NA

You should remove missing values before sub-setting, for example:

dat[complete.cases(dat),] 

Or

dat[!is.na(dat$subject_code),]

Upvotes: 3

Related Questions