user670301
user670301

Reputation: 23

Sum matrix rows with same index

I have an R matrix of the form

ID, string, float, int  
[...]  
2038 0;1;2;3;4;5;6;4;2;  898.990    325469692  
2040 0;1;2;3;4;5;6;4;2;  932.212    346769837  
2041 0;1;2;3;4;5;6;4;3; 1031.700    400210530  
2042 0;1;2;3;4;5;6;4;3; 1308.280    510633672  
2043 0;1;2;3;4;5;6;4;3; 1336.170    480728121  

basically I have a row with a strings with a lot of duplicates and I want to merge all rows with the same string by adding the respective row values.

The result should look like this:

XX  0;1;2;3;4;5;6;4;2; 1831.202    672239529  
XY  0;1;2;3;4;5;6;4;3; 3676.15     1391572323

What functions in R allow this aggregation?

Upvotes: 2

Views: 3076

Answers (2)

Chase
Chase

Reputation: 69201

This question is fundamentally the same as this question.

Accordingly:

library(plyr)
ddply(dat, "string", summarise, floatsum = sum(float), intsum = sum(int))

library(reshape)
cast(melt(dat[, -1]), string ~ ..., sum)

Upvotes: 2

mdsumner
mdsumner

Reputation: 29497

Example data:

dat <- read.table(textConnection("ID, string, float, int  
2038 0;1;2;3;4;5;6;4;2;  898.990    325469692  
2040 0;1;2;3;4;5;6;4;2;  932.212    346769837  
2041 0;1;2;3;4;5;6;4;3; 1031.700    400210530  
2042 0;1;2;3;4;5;6;4;3; 1308.280    510633672  
2043 0;1;2;3;4;5;6;4;3; 1336.170    480728121"), header = TRUE)

The result is a data.frame, not a matrix - a data.frame can have columns of different data types.

Drop the ID column:

dat <- dat[ , c("string.", "float.", "int")]

Now for aggregate by formula:

aggregate( . ~ string., data = dat, sum)
             string.   float.        int
1 0;1;2;3;4;5;6;4;2; 1831.202  672239529
2 0;1;2;3;4;5;6;4;3; 3676.150 1391572323

Upvotes: 4

Related Questions