user2960593
user2960593

Reputation: 87

Add values in cells of a dataframe based on a condition

I have a table that looks like this,

ID    c1    c2    c3    c4
A     23    12    45    63
A     3     1     6     17
B     3     1     4     6
B     2     2     5     3

and I would like to end up with something like this,

ID    c1    c2    c3    c4
A     26    13    51    80
B     5     3     9     9

where, each cell is a sum of values that map to the same id.

I would like to solve this using R. Any thoughts? I know that if wanted to sum all values in a column i can use colsums but I am not sure how to sum the values based on a criteria.

Any help will be appreciated.

Ram

P.S: The actual table I have has 45000 rows and 72 columns.

Upvotes: 3

Views: 930

Answers (3)

eddi
eddi

Reputation: 49448

For a faster alternative:

library(data.table)
dt = data.table(df)

dt[, lapply(.SD, sum), by = ID]
#   ID c1 c2 c3 c4
#1:  A 26 13 51 80
#2:  B  5  3  9  9

Upvotes: 2

Victorp
Victorp

Reputation: 13866

An other way with plyr

library(plyr)
ddply(x, .(ID), numcolwise(sum))

  ID c1 c2 c3 c4
1  A 26 13 51 80
2  B  5  3  9  9

Upvotes: 2

Fernando
Fernando

Reputation: 7905

Try

aggregate( . ~ ID, data = x, FUN = sum)

  ID c1 c2 c3 c4
1  A 26 13 51 80
2  B  5  3  9  9

Upvotes: 3

Related Questions