[R] subsetting by cell value with a list

G See gsee000 at gmail.com
Thu Mar 15 21:47:24 CET 2012


On second thought, I must not understand your question because your
code works fine for me.

> my.df[ which(my.df$IDX == c(17, 42)), ]
   IDX          x
17  17 -0.5110095
42  42 -1.0686427

> my.df[my.df$IDX == c(17, 42), ]
   IDX          x
17  17 -0.5110095
42  42 -1.0686427

Can you provide sample data?  What is the output of dput(head(my.df))


On Thu, Mar 15, 2012 at 3:38 PM, G See <gsee000 at gmail.com> wrote:
>> my.df <- data.frame(IDX=1:42, x=rnorm(1:42))
>> my.df[my.df$IDX %in% c(17, 42), ]
>   IDX          x
> 17  17 -0.5110095
> 42  42 -1.0686427
>
> Garrett
>
> On Thu, Mar 15, 2012 at 3:25 PM, Ed Siefker <ebs15242 at gmail.com> wrote:
>> I would like to subset by dataframe by matching all rows that have any value
>> from a list of values.  I can get it to work if I have exactly one value,
>> I'm not
>> sure how to do it with a list of values though.
>>
>> This works and gives me exactly one line:
>> my.df[ which( mydf$IDX==17)), ]
>>
>> I would like to do something like this:
>> my.df[ which( mydf$IDX==c(17, 42), ]
>>
>> Obviously that won't work, but I hope the meaning is clear.
>> What's the right way to express this?
>>
>>        [[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