【问题标题】:How to collapse values by group from unpaired elements in a data frame in R [duplicate]如何从R中数据框中的未配对元素按组折叠值[重复]
【发布时间】:2019-11-21 23:40:26
【问题描述】:

给定一个包含 4 列的数据框(我的数据框包含大约 40 个基因)

my_df <- read.table(header=T, text="
count   gene    group   sample
0.1 gene1   exp S_1
0.2 gene1   exp S_2
0.5 gene2   cnt S_1
0.6 gene2   exp S_1
0.2 gene2   exp S_2
0.4 gene1   cnt S_1
0.3 gene1   cnt S_2
0.2 gene1   cnt S_3

我想折叠每个基因的计数值并按组折叠(exp 和 cnt) 这是作为数据框的所需输出:

count   gene    group
0.3 gene1   exp
0.9 gene1   cnt
0.8 gene2   exp
0.5 gene2   cnt

一些规格: 可以删除列“样本”,因为它仅显示未配对中每个组的基因出现次数。 非常感谢您的建议

【问题讨论】:

  • aggregate(count~gene+group, my_df, sum)

标签: r dataframe collapse


【解决方案1】:
library(dplyr)
my_df %>% group_by(gene, group) %>% 
    summarise(count = sum(count))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多