1

Is there a specific R function that i can use to achieve the following? I tried using gather, but its not quite the same.

df3 <-data.frame(ID=c(1,1,2,2), ID2= c(11,11,22,21), ID3=c(22,22,33,33), b =c(5,0,0,0), c=c(0,0, 3,0), d=c(0L,3,0,4))
df3

#  ID ID2 ID3 b c d  
#1  1  11  22 5 0 0
#2  1  11  22 0 0 3
#3  2  22  33 0 3 0
#4  2  21  33 0 0 4


after_df3 <- data.frame(ID1=c(1,2,2), ID2=c(11,22,21), ID3=c(22,33,33), b=c(5,0,0), c=c(0,3,0), d=c(3,0,4))
after_df3

#   ID1 ID2 ID3 b c d
#1   1  11  22 5 0 3
#2   2  22  33 0 3 0
#3   2  21  33 0 0 4
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
flowy-data
  • 33
  • 1
  • 6
  • 2
    You need to summarize/aggregate, not gather. Using `dplyr`, it would be `df3 %>% group_by(ID) %>% summarise_each(funs(sum))`. And what is `output(...)`? – alistaire May 17 '16 at 06:07
  • please ignore output(df3), i havent figured out to include R output in the question. – flowy-data May 17 '16 at 06:22
  • The lines defining the data.frames are good, but just copy and paste the results of printing them as well (commented out, if you like). [Here's further reading about how to write a great R question.](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – alistaire May 17 '16 at 06:25
  • For the edit, in `dplyr` just add the grouping variables to `group_by`, i.e. `df3 %>% group_by(ID, ID2, ID3) %>% summarise_each(funs(sum))` – alistaire May 17 '16 at 06:36

1 Answers1

0

We can use aggregate from base R

dfN <- aggregate(.~ID, df3, sum)
dfN
#  ID b c d
#1  1 5 0 3
#2  2 0 3 4

Or using data.table

library(data.table)
dfN1 <- setDT(df3)[, lapply(.SD, sum), by = ID]
dfN1

Update

With the new dataset

aggregate(.~ID+ID2+ID3, df3, sum)
#  ID ID2 ID3 b c d
#1  1  11  22 5 0 3
#2  2  21  33 0 0 4
#3  2  22  33 0 3 0

Or using data.table

setDT(df3)[,lapply(.SD, sum) ,.(ID, ID2, ID3)]
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662