【发布时间】:2019-12-13 13:55:54
【问题描述】:
您好,给定以下数据框
library(tidyverse)
df <- data.frame(READS=rep(c('READa', 'READb', 'READc'),each=3) ,GENE=rep(c('GENEa', 'GENEb', 'GENEc'), each=3), COMMENT=rep(c('CommentA', 'CommentA', 'CommentA'),each=3))
> df
READS GENE COMMENT
1 READa GENEa CommentA
2 READa GENEa CommentA
3 READa GENEa CommentA
4 READb GENEb CommentA
5 READb GENEb CommentA
6 READb GENEb CommentA
7 READc GENEc CommentA
8 READc GENEc CommentA
9 READc GENEc CommentA
我想通过基因列将长格式聚合转换为宽格式,以便获得以下信息
GENEa GENEb GENEc
READSa 3 3 3
READSb 3 3 3
我试过没有成功:
library(tidyverse)
df %>%
group_by(GENE) %>%
select(-COMMENT) %>%
spread(READS)
请注意,原始数据框很大,因此任何优化的代码都会有所帮助。
感谢您的帮助。
【问题讨论】:
-
为什么
READsa和GENEc是3?不存在这样的组合。