[R] selection part of "subset"

peter dalgaard pdalgd at gmail.com
Thu Jan 5 17:26:15 CET 2012


On Jan 5, 2012, at 17:04 , Christof Kluß wrote:

> Hi
> 
> the output should look like     r <- subset(tab, a==v)
> but now I have something like   r <- subset(tab, "a"==v)
> and                             r <- subset(tab, [["a"]]==v)
> does not work :(

Presumably something with eval, bquote and as.name will work, but it's really not a great idea to use subset() in a programming context (notice also that if you accidentally have a variable tab$v, things might be much different than you expect.) Consider Sarah's suggestion instead, possibly throwing in a !is.na(). 

> 
> greetings
> Christof
> 
> Am 05-01-2012 16:51, schrieb Christof Kluß:
>> Hi
>> 
>> I want to do something like
>> 
>> a <- c(10,20,15,43,76,41,25,46)
>> tab <- data.frame(a)
>> 
>> name <- "a"
>> 
>> for (v in unique(tab[[name]])) {
>>  r <- subset(tab, name==v)   # this does not work
>>  ...
>> }
>> 
>> i.e. a "string" on the left side of the select expression (subset). How
>> could I solve this?
>> 
>> thx
>> Christof
>> 
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list