Skip to content
Prev 16985 / 398502 Next

Grouping and Computing

Generally, the data frame is the most useful object for storing data.  It
allows each column to have a different type (factor, numeric, ...).

This is the default object type returned from read.table(), read.csv(), etc.

If you have your data in a data file named "data.txt" in the format

Group  Value1  Value2
A	  1       2
A       1       3
B       2       3
B       1       3
C       1       1
...


you can read it into R with

data <- read.table("data.txt",header=T)

Now, to get the summary you want, you should 

  1) create a function to compute the summary for a data.frame containing
only the data for one group.  Something like

	compute.summary <- function(x)
            sum( x$Value1 / x$Value2 )

  2) Use 'split' to break the data frame into one chunk per group, and
'sapply' to call your function on each chunk:

	tmp <- split( data, data$Group )

	results <- sapply( tmp, compute.summary )

You will probably want to look at the help pages for read.table, split, and
sapply.  You should also (if you haven't already) picked up the manual 'An
Introduction to R' from http://cran.r-project.org/manuals.html

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


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._