[R] seeking help with with()
Simon Fear
Simon.Fear at synequanon.com
Wed Aug 27 14:30:26 CEST 2003
I tried to define a function like:
fnx <- function(x, by.vars=Month)
print(by(x, by.vars, summary))
But this doesn't work (does not find x$Month; unlike other functions,
such as
subset(), the INDICES argument to "by" does not look for variables in
dataset
x. Is fully documented, but I forget every time). So I tried using
"with":
fnxx <- function(x, by.vars=Month)
print(with(x, by(x, by.vars, summary)))
Still fails to find object x$Month.
I DO have a working solution (below) - this post is just to ask: Can
anyone
explain what happened to the with()?
FYI solutions are to call like this:
fnx(airquality, airquality$Month)
but this will not work generically - e.g. in my real application the
dataset
gets subsetted and by.vars needs to refer to the subsets. So redefine
like
this:
fny <- function(x, by.vars=Month) {
attach(x)
print(by(x, by.vars, summary))
detach(x)
}
Simon Fear
Senior Statistician
Syne qua non Ltd
Tel: +44 (0) 1379 644449
Fax: +44 (0) 1379 644445
email: Simon.Fear at synequanon.com
web: http://www.synequanon.com
Number of attachments included with this message: 0
This message (and any associated files) is confidential and\...{{dropped}}
More information about the R-help
mailing list