3

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.

0

3 Answers 3

3

Try

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

  ID c1 c2 c3 c4
1  A 26 13 51 80
2  B  5  3  9  9
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.