[R] subsetting by cell value with a list

David Winsemius dwinsemius at comcast.net
Thu Mar 15 22:47:35 CET 2012


On Mar 15, 2012, at 4:38 PM, G See 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

subset(my.df, IDX %in% c(17,42) )

(I find subset to give clearer code than: df[which(df$colname <logical- 
cond>) , ], plus it created its own environment. Downside is that it  
is recommended only for interactive use.)

-- 
David.
>
> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list