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! Thanx, Santosh [[alternative HTML version deleted]]
To further explain my case.. am reproducing one of the examples.from one of contributors' guides.. _______________ attach(ais) here<- sex=="f" plot(pcBfat[here]~ht[here], xlab = “Height”, ylab = “% Body fat”) panel.smooth(ht[here],pcBfat[here]) detach(ais) ______________ A condition is in "here",... I am not able to use a similar conditioning as an argument to a function.. Any suggestions will be highly appreciated! Thnx.. Santosh On Fri, Nov 20, 2009 at 5:40 PM, Santosh <santosh2005@gmail.com> 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! > > Thanx, > Santosh > >[[alternative HTML version deleted]]
Bernardo Rangel Tura
2009-Nov-21 08:04 UTC
[R] "subset" or "condition" as argument to a function
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!Hi Santosh, IMHO your script is pltit <- function(y,x,dat,dat1,dat2,sbst) { subs <- subset(dat,sbst) with(subs,plot(y~x)) subs1 <- subset(dat1,sbst) with(subs1,lines(y~x)) subs2 <- subset(dat2,sbst) with(subs2,points(y~x)) } -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
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') 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@gmail.com> wrote:> Thanks... I would try it out.. > -santosh > > > On Sat, Nov 21, 2009 at 12:04 AM, Bernardo Rangel Tura < > tura@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]]