[R] Using 'by()' in a function

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Apr 28 20:08:58 CEST 2000


Setzer.Woodrow at epamail.epa.gov writes:

> I have a list of dataframes, and want to apply a function to subsets of the rows
> of each dataframe.  It seemed natural to write a function that takes a dataframe
> as an argument, and uses 'by() within it to apply the function to the dataframe
> subsets.  However, I cannot get it to work.  The problem seems to be passing the
> data argument of by() as a function argument.  Is this bug, or am I missing
> something (or both)?

It's a bug: This looks like the culprit

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

That will substitute the unevaluated data argument into the
"tapply(1:nrow(data),...)" where it will not make sense except at top
level. 

I think we want 

    NN<-seq(len=nrow(data))
    ans <- eval(substitute(tapply(NN, IND, FUNx)), data)

(but I didn't test that)

-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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