Skip to content

Is there a "by()" function in R like in S?

2 messages · Sven Garbade, Peter Dalgaard

#
Hallo R-users,

I'm a R (release 0.65) novice, but have read some about S. Is there a
similar function in R like the S function "by()" ?
This function groups data typically from a matrix by a variable and
applies a function on the grouped data. Example in S:

  > by(data, year, summary)

where "data" is a matrix containing the variable "year". After grouping
the matrix to variable "year", the function "summary" is applied to each
group.

Thanks,
Sven

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Sven Garbade <garbade at mip.paed.uni-muenchen.de> writes:
It's not in there, but it shouldn't be too hard to write one. Here's
a start:

by <- function(data, INDICES, FUN, ...)
{
   ee <- substitute(tapply(1:nrow(data), INDICES, FUNx))
   FUNx <- function(x) FUN(data[x,], ...)
   ans <- eval(ee, data)
   class(ans)<-"by"
} 

Now go and write print.by ...