Dear R-users, The following question bothered me for the whole afternoon: how can one pass a string as the conditioning argument to subset? I tried plain mystr, eval(mystr), expression(mystr), etc... I don't to be able to find the correct syntax > foo <- data.frame(a=1:10,b=10:1,c=rep(1:2,5)) > mystr<-"c==1" > subset(foo,c==1) a b c 1 1 10 1 3 3 8 1 5 5 6 1 7 7 4 1 9 9 2 1 > subset(foo,mystr) Error in subset.data.frame(foo, mystr) : 'subset' must evaluate to logical Any help would be greatly appreciated. Sebastien
Try this, mystr <-"c==1" subset(foo, eval(parse(text = mystr)) ) library(fortunes) fortune("parse") # try several times # I prefer this, but there is probably a better way mycond<- quote(c==1) subset(foo, eval(bquote(.(mycond))) ) HTH, baptiste 2009/8/21 Sebastien Bihorel <sebastien.bihorel at cognigencorp.com>:> Dear R-users, > > The following question bothered me for the whole afternoon: how can one pass > a string as the conditioning argument to subset? I tried plain mystr, > eval(mystr), expression(mystr), etc... I don't to be able to find the > correct syntax > >> foo <- data.frame(a=1:10,b=10:1,c=rep(1:2,5)) >> mystr<-"c==1" >> subset(foo,c==1) > ?a ?b c > 1 1 10 1 > 3 3 ?8 1 > 5 5 ?6 1 > 7 7 ?4 1 > 9 9 ?2 1 >> subset(foo,mystr) > Error in subset.data.frame(foo, mystr) : > ?'subset' must evaluate to logical > > > > Any help would be greatly appreciated. > > Sebastien > > ______________________________________________ > 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. >-- _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag
Sebastien, My understanding is the argument of c==1 in subset() is actually to evaluate foo$c==1, which returns a vector of logical values. Try this, mystr<-foo$c==1 subset(foo,mystr) Jun On Fri, Aug 21, 2009 at 3:21 PM, Sebastien Bihorel < sebastien.bihorel@cognigencorp.com> wrote:> Dear R-users, > > The following question bothered me for the whole afternoon: how can one > pass a string as the conditioning argument to subset? I tried plain mystr, > eval(mystr), expression(mystr), etc... I don't to be able to find the > correct syntax > > > foo <- data.frame(a=1:10,b=10:1,c=rep(1:2,5)) > > mystr<-"c==1" > > subset(foo,c==1) > a b c > 1 1 10 1 > 3 3 8 1 > 5 5 6 1 > 7 7 4 1 > 9 9 2 1 > > subset(foo,mystr) > Error in subset.data.frame(foo, mystr) : > 'subset' must evaluate to logical > > > > Any help would be greatly appreciated. > > Sebastien > > ______________________________________________ > R-help@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. >-- Jun Shen PhD PK/PD Scientist BioPharma Services Millipore Corporation 15 Research Park Dr. St Charles, MO 63304 Direct: 636-720-1589 [[alternative HTML version deleted]]