Hello,

 From the help page for
?`==`


      Note

Do not use |==| and |!=| for tests, such as in |if| expressions, where 
you must get a single |TRUE| or |FALSE|. Unless you are absolutely sure 
that nothing unusual can happen, you should use the |identical 
<http://127.0.0.1:16370/library/base/help/identical>| function instead.

inx <- sapply(a$y, identical, 0)
inx
  [1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE
  a[inx, ]
     x y
2   2 0
5   5 0
10 10 0

Or, I think better because it's simpler:

not.na <- !is.na(a$y)
a[not.na & a$y == 0, ]
     x y
2   2 0
5   5 0
10 10 0

Hope this helps,

Rui Barradas

Em 15-08-2012 21:06, Robin Jeffries escreveu:
> 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)
> rjeffr...@ucla.edu
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to