[R] Problem with passing a string to subset

Sebastien Bihorel sebastien.bihorel at cognigencorp.com
Fri Aug 21 22:21:22 CEST 2009


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




More information about the R-help mailing list