[R] "subset" or "condition" as argument to a function

baptiste auguie baptiste.auguie at googlemail.com
Tue Dec 1 18:36:30 CET 2009


Hi,

an alternative to parse() is to use quote and bquote,

set.seed(123)
d = data.frame(a=letters[1:5], b=1:10, c=sample(0:1, 10, repl=TRUE))

cond1 <- quote(a=="b")
cond2 <- quote(b < 6)
cond3 <- bquote(.(cond1) & .(cond2))

subset(d, eval(cond1))
subset(d, eval(cond2))
subset(d, eval(cond3))

HTH,

baptiste



2009/12/1 William Dunlap <wdunlap at tibco.com>:
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Santosh
>> Sent: Tuesday, December 01, 2009 7:39 AM
>> To: r-help at r-project.org
>> Subject: Re: [R] "subset" or "condition" as argument to a function
>>
>> Dear R gurus..
>> I had tried out some suggestions sent to me privately..and
>> unfortunately,
>> they did not work..
>>
>> To use a condiition in a subset, the associated dataframe needs to be
>> attached and detached, which I found cumbersome to use if
>> using more than 1
>> dataframe (with different dimensions) with same condition. I
>> would highly
>> appreciate your suggestions to overcome this problem.. Please
>> see my example
>> of usage I am trying to implement. Please note that the
>> "cond' takes in the
>> character instead of logical values...
>>
>> cond1 <- 'group==1&sales>200'
>> cond2 <- 'group==2&sales>200'
>> cond3 <- 'group==3&sales>200'
>>
>> d1 <- subset(dat,subset=cond1)
>> plot(y~x,data=dat,subset=cond1,type='b')
>
> The subset argument to plot (and many similar functions)
> must be given as a literal expression, not a string that
> could be parsed into an expression nor the name of an object
> containing an expression nor a function call that evaluates
> to an expression.  This design is handy for interactive
> use but painful in programatic use.  One way to deal with
> it is to use do.call, which evaluates all the arguments to
> the function and then calls the function with the arguments
> given literally.  Replace the above plot call with
>  do.call("plot", list(y~x,data=dat,subset=parse(text=cond1),type='b'))
> and see if you get what you want.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>> lines(y~x,data=dat,subset=cond2)
>> points(y~x,data=dat,subset=paste(cond1,cond2,sep='&'),col='blue')
>> points(y~x,data=d1,subset=cond3,col='red')
>>
>> If I try the above, I get the following error message:
>> *Error in subset.data.frame(dat, cond) : 'subset' must
>> evaluate to logical*
>>
>> If you could also suggest references implementing similar
>> code, I would
>> highly appreciate it.
>> Thanks so much,
>>
>> Santosh
>>
>> On Mon, Nov 23, 2009 at 6:38 PM, Santosh
>> <santosh2005 at gmail.com> wrote:
>>
>> > Thanks... I would try it out..
>> >  -santosh
>> >
>> >
>> > On Sat, Nov 21, 2009 at 12:04 AM, Bernardo Rangel Tura <
>> > tura at centroin.com.br> wrote:
>> >
>> >> On Fri, 2009-11-20 at 17:40 -0800, Santosh wrote:
>> >> > Dear Rxperts!
>> >> >
>> >> > I was wondering if it is possible to write a function
>> which can take in
>> >> > argument of a subset or condition.. Of course, I am aware of the
>> >> alternate
>> >> > methods like coplot, par.plot, xyplot etc... I am specifically
>> >> interested in
>> >> > using conditions/subsets with "plot"..
>> >> >
>> >> > A simple fragmented example is shown here..
>> >> >
>> >> > pltit <- function(y,x,dat,dat1,dat2,sbst) {
>> >> > plot(y~x, data=dat, subset=sbst)
>> >> > lines(y~x,data=dat1, subset=subst)
>> >> > points(y~x,data=dat2,subset=subst)
>> >> > }
>> >> >
>> >> > pltit(profit,weeks,dat=zone1, sbst='group==1&sales>200')
>> >> >
>> >> > Could you also suggest pointers/references/examples on
>> efficient ways to
>> >> > plot simulated data overlaid with observed data?
>> >> >
>> >> > Have a good weekend!
>> >>
>> >>
>> >>
>> >
>>
>>       [[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.
>




More information about the R-help mailing list