[R] "subset" or "condition" as argument to a function
David Winsemius
dwinsemius at comcast.net
Tue Dec 1 16:59:49 CET 2009
On Dec 1, 2009, at 10:55 AM, David Winsemius wrote:
>
> On Dec 1, 2009, at 10:38 AM, Santosh wrote:
>
>> 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'
>
> I do not see that you have _ever_ offered a sample dataset in this
> thread, so this suggestion remains untested. Please report what
> happens when you try:
>
>>
>> d1 <- subset(dat,subset=eval(parse(text=cond1)))
>> plot(y~x,data=dat,subset=eval(parse(text=cond1)),type='b')
>> lines(y~x,data=dat,subset=eval(parse(text=cond2)))
>> points
>> (y
>> ~
>> x
>> ,data
>> =dat,subset=eval(parse(text=paste(cond1,cond2)),sep='&'),col='blue')
Probably needs to be tested with this modification:
points(y~x, data=dat, subset=eval(parse(text=paste(cond1,cond2,
sep='&'))), col='blue')
>> points(y~x,data=d1,subset=eval(parse(text=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.
>
> ?parse
>
>> 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!
>>>>
>>>>
>>>
> --
>> .
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> 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
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list