一個函數應用於分組的行我具有由如何R中
points_A = sample(1:6,6)
points_B = sample(1:6,6)
points_C = sample(1:6,6)
df <- data.frame(name = gl(3,2,labels=c("Luca","Mario","Paolo")) , cbind(points_A,points_B,points_C) )
其中顯示作爲
name points_A points_B points_C
1 Luca 5 2 3
2 Luca 3 3 1
3 Mario 1 5 2
4 Mario 6 6 4
5 Paolo 4 4 5
6 Paolo 2 1 6
我想應用功能而產生的數據幀(例如總和())到按列名稱(第1列)分組的行。
輸出應該是這樣的:
name points_A points_B points_C
1 Luca 8 5 4
2 Mario 7 11 6
3 Paolo 6 5 11
有什麼建議?
'骨料(〜名,DF,總和)',或在dplyr,'df%>%group_by(name)%>%summarise_all(sum)' – alistaire
'aggregate(。〜name,df,sum)'這是行得通的,但是如果我有其他列如lastname,city等,我仍然想要在數據框中,但我不想傳遞給聚合,因爲它們是另一個dat一種?有沒有一種方法可以指定哪些內容? – alevax
[這解決了我以前的評論](http://stackoverflow.com/questions/15978985/how-to-aggregate-some-columns-while-keeping-other-columns-in-r) – alevax