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
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
It makes sense if you think it through. Your index vector is
a$y==0
[1] FALSE TRUENA FALSE TRUENANA 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
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 rjeffr...@ucla.edu
To: r-help@r-project.org
Cc:
Sent: Wednesday, August 15, 2012 4:06 PM
Subject: [R] Subsetting
4 matches
Mail list logo