【问题标题】:Reshaping then collapsing dataframes重塑然后折叠数据框
【发布时间】:2018-07-23 13:56:02
【问题描述】:

努力将我凌乱、长度不等的data.frame 从宽表转换为长表,然后折叠(汇总)新变量。目前看起来是这样的,Gene 作为一个变量,GO_terms 作为一个包含多个逗号分隔值的变量:

Gene            GO_terms                        
AA1006G00001    GO:0098655, GO:0008643, GO:0005351, GO:0005886, GO:0016021      
AA100G00001     GO:0098655, GO:0009944, GO:0009862, GO:0010075, GO:0010014, GO:0009855, GO:0010310
AA100G00002     GO:0098655, GO:0008643, GO:0005886

我要做的第一步是转换为“长”格式,所以它看起来像这样:

Gene            GO_terms 
AA1006G00001    GO:0098655
AA1006G00001    GO:0008643
AA1006G00001    GO:0005351
AA1006G00001    GO:0005886
AA1006G00001    GO:0016021
AA100G00001     GO:0001666
AA100G00001     GO:0009944
AA100G00001     GO:0009862
AA100G00001     GO:0010075
AA100G00001     GO:0010014
AA100G00001     GO:0009855
AA100G00001     GO:0010310
AA100G00002     GO:0008270
AA100G00002     GO:0005634
AA100G00002     GO:0005886
AA100G00003     GO:0005488
AA100G00003     GO:0005634

然后,我希望通过切换两个变量来重新组织这个data.table,因为它的整理如下:

GO_terms    Genes
GO:0005351  AA1006G00001        
GO:0005886  AA1006G00001,   AA100G00002 
GO:0008643  AA1006G00001,   AA100G00002 
GO:0009855  AA100G00001     
GO:0009862  AA100G00001     
GO:0009944  AA100G00001     
GO:0010014  AA100G00001     
GO:0010075  AA100G00001     
GO:0010310  AA100G00001     
GO:0016021  AA1006G00001        
GO:0098655  AA1006G00001,   AA100G00001,      AA100G00002

包含基因的变量可以在一列中(使用逗号分隔值),也可以在多列中。

谁能提供tidyrreshape2dplyr 解决方案?

编辑:dput() 表是:

structure(list(`Gene    ` = c("AA1006G00001\t", "AA100G00001\t", 
"AA100G00002\t"), `GO_terms                     ` = c("GO:0098655, GO:0008643, GO:0005351, GO:0005886, GO:0016021\t\t", 
"GO:0098655, GO:0009944, GO:0009862, GO:0010075, GO:0010014, GO:0009855, GO:0010310", 
"GO:0098655, GO:0008643, GO:0005886")), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
    cols = list(`Gene   ` = structure(list(), class = c("collector_character", 
    "collector")), `GO_terms                        ` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector"))), class = "col_spec"))

【问题讨论】:

  • 到目前为止您尝试过什么?你在什么时候坚持?我认为 SO 对您的每个部分问题都有很多答案。
  • 你能像dput一样发布第一个数据框吗?这会更有帮助,因为您正在处理我们无法从复制和粘贴文本中得到的结构问题
  • @AndreElrico 我尝试先按列分隔 GO_terms,然后使用 Gene 变量尝试 melt()。结果不正确,有很多空白值(因为我的 GO_terms 长度不等?)。

标签: r dataframe dplyr tidyr reshape2


【解决方案1】:

看来您正在做一些 GO 分析。您可以从topGO(Bioconductor 中最流行的 GO 分析 R 包之一)尝试inverseList

library(topGO)

gene.to.go <- strsplit(gsub('\t', '', df$GO_terms), ', ', fixed = TRUE)
names(gene.to.go) <- gsub('\t', '', df$Gene)

go.to.gene <- inverseList(gene.to.go)

data.frame(GO_term = names(go.to.gene), Genes = sapply(go.to.gene, paste0, collapse = ', '),
           stringsAsFactors = FALSE, row.names = NULL)

#       GO_term                                  Genes
# 1  GO:0005351                           AA1006G00001
# 2  GO:0005886              AA1006G00001, AA100G00002
# 3  GO:0008643              AA1006G00001, AA100G00002
# 4  GO:0009855                            AA100G00001
# 5  GO:0009862                            AA100G00001
# 6  GO:0009944                            AA100G00001
# 7  GO:0010014                            AA100G00001
# 8  GO:0010075                            AA100G00001
# 9  GO:0010310                            AA100G00001
# 10 GO:0016021                           AA1006G00001
# 11 GO:0098655 AA1006G00001, AA100G00001, AA100G00002

其实在topGO中导入带有readMappings的GO映射文件会更容易对数据进行操作。

【讨论】:

  • 谢谢@mt1022!你说得对——我正在做一些 GO 分析。我使用readMappings 导入了数据,随后使用inverseList 对其进行了反转。它似乎不想导出为表格。任何想法为什么会这样?
  • 啊,将新的反转对象重新分配给data.table!非常感谢您的帮助@mt1022!
【解决方案2】:

这是一个 tidyr 和 dplyr 的解决方案:

library(tidyr)
library(dplyr)

#allow up to seven Genes per GO_term if there is more increase the letters expression
long<-df %>% separate(GO_terms, into=paste0("a", 1:100), sep=", ", extra="merge") %>% 
  gather( key="key", value="GO_terms", -Gene)

#filter data frame, remove the NA and keep the desired columns
long<-long[!is.na(long$GO_terms), c("Gene", "GO_terms")]

final<-long %>% group_by(GO_terms) %>% summarize( Gene=toString(Gene) )

【讨论】:

  • 谢谢,@Dave2e。奇怪的是,这种方法产生了 5963 个唯一的 GO_terms,而 @mt1022 将 Mappings 文件反转为 TopGo 的方法产生了 5994 个唯一的 GO_terms。奇怪...有什么想法吗?
  • 是的,当然,我只上传了data.table 的一小部分,用于 SO!将 into=letters 参数更改为 1:100(我知道我的 GO_terms 的最大数量是 56)仍然产生相同的数字。
  • @WaheedArshad 56 个元素,我完全低估了那个。我编辑了上面的代码以允许多达 100 个不同的 GO_terms。以前的版本只接受前 26 个。
  • 你是明星,@Dave2e!非常感谢 - 非常感谢!
猜你喜欢
  • 2018-07-18
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2013-01-27
  • 2015-10-07
相关资源
最近更新 更多