2017-05-28 57 views
0

我有一個data.frame,看起來像這樣(但具有較大數量的列和行):如何聚合數據幀以多列重複的行

Gene  Cell1 Cell2 Cell3  
1  A   2  7  8 
2  A   5  2  9 
3  B   2  7  8 
4  C   1  4  3 

我要總結的是有行在Gene相同的值,爲了得到這樣的:

Gene  Cell1 Cell2 Cell3  
1  A   7  9  17 
2  B   2  7  8 
3  C   1  4  3 

基礎上回答前面的問題,我試圖用aggregate,但我不明白我怎麼能得到上述結果。這是我試過的:

aggregate(df[,-1], list(df[,1]), FUN = sum) 

有沒有人有我的做法錯誤的想法?

+0

這有什麼錯,你已經與聚合得到的結果呢? – Bea

回答

2
aggregate(df[,-1], list(Gene=df[,1]), FUN = sum) 
# Gene Cell1 Cell2 Cell3 
# 1 A  7  9 17 
# 2 B  2  7  8 
# 3 C  1  4  3 

會給你你正在尋找的輸出。

+0

有一個錯誤,當我們運行上面的代碼時:'aggregate.data.frame(df [,-1],list(Gene = df [,1])中的錯誤,FUN = sum): 參數必須具有相同的長度' –

+0

@ManojKumar請將'str(df)'的輸出添加到您的文章中。 – lukeA

+0

確實@lukeA在這裏它是:'類'data.table'和'data.frame':\t 4 obs。 4變量: $基因:字母「A」「A」「B」「C」 $ Cell1:int 2 5 2 1 $ Cell2:int 7 2 7 4 $ Cell3:int 8 9 8 3 - attr(*,「.internal.selfref」)= ' –

1

或者與dplyr

library(dplyr) 
df %>% 
    group_by(Gene) %>% 
    summarise_all(sum) %>% 
    data.frame() -> newdf # so that newdf can further be used, if needed