2017-04-23 53 views
2

聚集的我有一個數據幀,看起來像這樣:不同類型中的R

sub = c("X001","X001", "X001","X002","X002","X001","X002","X001","X002","X002","X002","X002") 
revenue = c(20, 15, -10,-25,20,-20, 17,9,14,12, -9, 11) 

df = data.frame(sub, revenue) 

我想要聚合它以這樣一種方式,第二欄應該顯示所有收入的總和爲子,第三列應顯示絕對值的總和,第四列應顯示所有正值的總和,第五列應顯示所有負值的總和。

結果應該是這樣的:

Sub  All Sum  Absolute Sum  Positive Sum  Negative Sum 

X001  14   74     44     -30 
X002  40   108    74     -34 

我已經寫了計算所有總和代碼:

y<-aggregate(df$revenue, by=list(Feature=x$Sub), FUN=sum) 

我真的很感激,如果有人中的R更見地會幫我在計算其他三列。

回答

3

以下是如何與dplyr做:

library(dplyr) 
df%>% 
group_by(sub)%>% 
summarise(All_Sum=sum(revenue),Absolute_Sum=sum(abs(revenue)), 
      Positive_Sum=(sum(revenue[revenue>0])),Negative_Sum=(sum(revenue[revenue<0]))) 

    sub All_Sum Absolute_Sum Positive_Sum Negative_Sum 
    <fctr> <dbl>  <dbl>  <dbl>  <dbl> 
1 X001  14   74   44   -30 
2 X002  40   108   74   -34 
+0

三江源,它的偉大工程:) –

1

在基礎R使用aggregate

aggregate(.~sub, df, function(a) c(sum(a), sum(abs(a)), sum(a[a>0]), sum(a[a<0]))) 

# sub revenue.1 revenue.2 revenue.3 revenue.4 
#1 X001  14  74  44  -30 
#2 X002  40  108  74  -34 
+1

這真的很酷,我不知道聚合函數可以像這樣擴展。謝謝 –

0

我們也可以使用data.table

library(data.table) 
setDT(df)[, .(All_Sum = sum(revenue), Absolute_Sum = sum(abs(revenue)), 
    Positive_Sum = sum(revenue[revenue>0]), Negative_Sum = sum(revenue[revenue<0])), by = sub] 
# sub All_Sum Absolute_Sum Positive_Sum Negative_Sum 
#1: X001  14   74   44   -30 
#2: X002  40   108   74   -34