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

Bernardo Rangel Tura tura at centroin.com.br
Sat Nov 21 09:04:33 CET 2009


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




More information about the R-help mailing list