如何按组折叠 R 中数据框中未配对元素的值
How to collapse values by group from unpaired elements in a data frame in R
给定一个包含 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
一些规格:
Colum "sample" 可以删除,因为它只是显示未配对中每个组的基因出现次数。
非常感谢您的建议
library(dplyr)
my_df %>% group_by(gene, group) %>%
summarise(count = sum(count))
给定一个包含 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
一些规格: Colum "sample" 可以删除,因为它只是显示未配对中每个组的基因出现次数。 非常感谢您的建议
library(dplyr)
my_df %>% group_by(gene, group) %>%
summarise(count = sum(count))