[R] seeking help with with()
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Wed Aug 27 15:26:28 CEST 2003
"Simon Fear" <Simon.Fear at synequanon.com> writes:
> 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()?
>
Nothing, but by.vars is evaluated in the function frame where it is
not defined. I think you're looking for something like
function(x, by.vars) {
if (missing(by.vars)) by.vars <- as.name("Month")
print(eval.parent(substitute(with(x, by(x, by.vars, summary)))))
}
(Defining the default arg requires a bit of sneakiness...)
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list