[R] Subset and levels

Christos Hatzis christos at nuverabio.com
Mon Nov 6 20:29:21 CET 2006


Hi Florent,

A simple example with the expected output would help.  If I understood
correctly what you want, perhaps the following would work:

X[ X$code %in% levels(Y$code), ]

assuming Y$code is a factor.  If not, you can used instead unique(Y$code) in
the above.

-Christos 

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Florent Bresson
Sent: Monday, November 06, 2006 12:15 PM
To: R-help
Subject: [R] Subset and levels

Hi, I've got a very simple problem but cannot find the solution. I'm using
two data frames (say X and Y) and I want to get a subset of one according to
the different levels of a variable "code" of the other data frame. I tried
something like
Z    <-    subset(X, code==levels(Y$code))        (1)

but it does not work. I do not want to do something like
Z    <-    subset(X,code==level1 | code==level2...)

because length(levels(Y$code)) is 130. So what's wrong with (1)    ?

Thanks

______________________________________________
R-help at stat.math.ethz.ch 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