[R] Subsetting with missing data

arun smartpink111 at yahoo.com
Wed Aug 15 23:27:29 CEST 2012


HI,

Try this:
subset(a,y==0)
#    x y
#2   2 0
#5   5 0
#10 10 0

#or
subset(a,y%in%0)
#    x y
#2   2 0
#5   5 0
#10 10 0


A.K.



----- Original Message -----
From: Robin Jeffries <rjeffries at ucla.edu>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, August 15, 2012 4:06 PM
Subject: [R] Subsetting with missing data

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.




More information about the R-help mailing list