2017-05-28 30 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