[R] using by() in a function

Setzer.Woodrow@epamail.epa.gov 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:

> get("by.data.frame",3)
function (data, INDICES, FUN, ...)
{
### code skipped to save space
   ans <- eval(substitute(tapply(1:nrow(data), IND, FUNx)),   ### The problem
seems to be here
        data)
    attr(ans, "call") <- match.call()
    class(ans) <- "by"
    ans
}
>

My change is:

    Nseq <- 1:nrow(data)
    ans <- eval(substitute(tapply(Nseq, IND, FUNx)),
        data)

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:
(919) 541-0128
Biostatistics and
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 mailing list