Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R- Collapse rows and sum the values in the column

I have the following dataframe (df1):

ID    someText    PSM OtherValues
ABC   c   2   qwe
CCC   v   3   wer
DDD   b   56  ert
EEE   m   78  yu
FFF   sw  1   io
GGG   e   90  gv
CCC   r   34  scf
CCC   t   21  fvb
KOO   y   45  hffd
EEE   u   2   asd
LLL   i   4   dlm
ZZZ   i   8   zzas

I would like to collapse the first column and add the corresponding PSM values and I would like to get the following output:

ID  Sum PSM
ABC 2
CCC 58
DDD 56
EEE 80
FFF 1
GGG 90
KOO 45
LLL 4
ZZZ 8

It seems doable with aggregate function but don't know the syntax. Any help is really appreciated! Thanks.

like image 813
RnD Avatar asked May 27 '13 17:05

RnD


4 Answers

Using data.table

setDT(df1)[,  lapply(.SD, sum) , by = ID, .SDcols = "PSM" ]
like image 167
Ferroao Avatar answered Sep 28 '22 05:09

Ferroao


Example using dplyr, the next iteration of plyr:

df2 <- df1 %>% group_by(ID) %>%
     summarize(Sum_PSM = sum(PSM))

When you put the characters %>%, you are "piping." This means you're inputting what is on the left side of that pipe operator and performing the function on the right.

like image 28
Chelsea Avatar answered Nov 09 '22 07:11

Chelsea


In base:

aggregate(PSM ~ ID, data=x, FUN=sum)
##    ID PSM
## 1 ABC   2
## 2 CCC  58
## 3 DDD  56
## 4 EEE  80
## 5 FFF   1
## 6 GGG  90
## 7 KOO  45
## 8 LLL   4
## 9 ZZZ   8
like image 21
Matthew Lundberg Avatar answered Nov 09 '22 06:11

Matthew Lundberg


This is super easy using the plyr package:

library(plyr)
ddply(df1, .(ID), summarize, Sum=sum(PSM))
like image 2
Noam Ross Avatar answered Nov 09 '22 06:11

Noam Ross