[R] Sum efficiently from large matrix according to re-occuring levels of factor?

From: Ralph S. <ruffel1_at_hotmail.com>
Date: Sun, 20 Jul 2008 23:16:08 +0000

Hi,

I am trying to calculate the sum for each occurrence of the level of a factor in a very large matrix. In addition, I want to save that sum together with the information of the level of the factor and the level of a second factor.

My matrix looks like this:

x<-matrix(c(1,1,1,2,2,3,3,1,1,7,7,7,4,4,2,2,7,7,1,1,1,1,1,1,2,5,5),9,3)

I want to sum according to the levels in the first column and save the sum with the information of the level in the first and the second column in a new matrix.

That is, I want output in the matrix of form:

1 7 3
2 4 2
3 2 3

1 7 10

The important thing is, that a factor level such as "1" in the example can re-occur many times. There are no regularities on the number of re-occurences etc.

How could I do this efficiently (the matrix is large:>10^6 rows)?

Many thanks!!

-Ralph



[[elided Hotmail spam]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 20 Jul 2008 - 23:21:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jul 2008 - 00:32:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive