[R] how can i have the total for each level using table,ftable,xtabs etc. etc. ?

Kaspar Pflugshaupt pflugshaupt at geobot.umnw.ethz.ch
Tue Dec 11 13:25:46 CET 2001

On 11.12.2001 12:54 Uhr, Danilo Maurizio wrote:

> I have a list with four fields and 3500 records, from a MySQL table.
> How can i have the total for each level using table, ftable, xtabs?

I'm not sure I've understood how your data is organized. If you have it
within a dataframe (which I recommend), you could use tapply():

tapply(mydata$datafield, list(mydata$field1, mydata$field2), sum)

The first argument is the column containing the data to be summed up. The
second argument lists all the factors (1..n) for whose levels you want the
sum calculated. You will get a sum for each combination of levels. For
details, see help(tapply).

A similar function is by(), but I haven't used it yet. I think it gives
nicer output.


Kaspar Pflugshaupt


Kaspar Pflugshaupt
Geobotanisches Institut
Zuerichbergstr. 38
CH-8044 Zuerich

Tel. ++41 1 632 43 19
Fax  ++41 1 632 12 15

mailto:pflugshaupt at geobot.umnw.ethz.ch
privat:pflugshaupt at mails.ch

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