[R] using by() in a function
Setzer.Woodrow at epamail.epa.gov
Fri Apr 28 19:44:16 CEST 2000
I have a fix to by.data.frame() that works for my example. Can anyone see a
problem with this?
The old code has:
function (data, INDICES, FUN, ...)
### code skipped to save space
ans <- eval(substitute(tapply(1:nrow(data), IND, FUNx)), ### The problem
seems to be here
attr(ans, "call") <- match.call()
class(ans) <- "by"
My change is:
Nseq <- 1:nrow(data)
ans <- eval(substitute(tapply(Nseq, IND, FUNx)),
I think what happens in the original is that 'data' gets replaced by 'z' (if
'by()' is called by function(z) by(data=z,...)) in the call to substitute. When
by(data=z,IND,FUN) is called at the toplevel, 'z' is a real data frame, and the
call to eval() does the right thing. If 'by(data=z,IND,FUN)' is called anywhere
else, 'z' will not be in 'sys.frame(sys.parent())', where eval is looking for
it, so 'z' remains unresolved.
R. Woodrow Setzer, Jr. Phone:
Fax: (919) 541-4002
Research Support Staff
NHEERL MD-55; US EPA; RTP, NC 27711
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help