Subsetting with missing data
It makes sense if you think it through. Your index vector is
a$y==0
[1] FALSE TRUE NA FALSE TRUE NA NA FALSE FALSE TRUE
and ?"[" says
NAs in indexing:
When extracting, a numerical, logical or character 'NA' index
picks an unknown element and so returns 'NA' in the corresponding
element of a logical, integer, numeric, complex or character
result, and 'NULL' for a list. (It returns '00' for a raw
result.]
so this is what one has to expect. Here are a couple alternatives for
getting what you want.
a[which(a$y==0),]
a[a$y %in% 0,]
Best,
Ista
On Wed, Aug 15, 2012 at 4:06 PM, Robin Jeffries <rjeffries at ucla.edu> wrote:
Simply put, I want to subset the data frame 'a' where 'y=0'.
a <- as.data.frame(cbind(x=1:10, y=c(1,0,NA,1,0,NA,NA,1,1,0))) a
x y 1 1 1 2 2 0 3 3 NA 4 4 1 5 5 0 6 6 NA 7 7 NA 8 8 1 9 9 1 10 10 0
names(a)
[1] "x" "y"
table(a$y)
0 1 3 4
table(a$y, useNA="always")
0 1 <NA> 3 4 3
b <- a[a$y==0,] b
x y 2 2 0 NA NA NA 5 5 0 NA.1 NA NA NA.2 NA NA 10 10 0
is(a$y)
[1] "numeric" "vector"
Instead of only pulling the rows where a$y==0, i'm getting where they're 0,
OR NA. ? Again I feel like either something was changed when I wasn't
looking.. or I'm reaaaaaaly forgetting something important.
Thanks,
Robin Jeffries
MS, DrPH Candidate
Department of Biostatistics,
UCLA
530-633-STAT(7828)
rjeffries at ucla.edu
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.