简体   繁体   English

将列表元素追加到数据框列

[英]Append list elements to data frame column

I have this dataframe: 我有这个数据框:

       GO.ID Annotated Significant Expected P-value                   Term Ontology
1 GO:0000049         7           0     0.25  1.0000           tRNA binding       MF
2 GO:0000062         4           0     0.14  1.0000 fatty-acyl-CoA binding       MF

And I have this list: 我有这个清单:

$`GO:0000049`
[1] "Solyc02g090860.2" "Solyc03g119280.2" "Solyc05g056260.2" "Solyc06g048610.2" "Solyc07g008950.2" "Solyc08g015960.2"
[7] "Solyc10g007060.2"

$`GO:0000062`
[1] "Solyc01g099350.2" "Solyc03g082910.2" "Solyc04g078090.2" "Solyc08g075690.2"

Is there any way to print the elements of the list to a new column of the data frame? 有什么方法可以将列表的元素打印到数据框的新列中? The order is the same in both structures, I mean, the GO.ID column is ordered as the list elements. 两种结构的顺序相同,我的意思是,GO.ID列作为列表元素进行排序。 I'm looking for something like paste bash command. 我在寻找粘贴bash命令之类的东西。

I've tried lapply and export the list to a file. 我尝试过lapply并将列表导出到文件中。 Then write.table with the dataframe and then paste command in bash. 然后使用write.table ,然后将命令paste到bash中。 But I'm wondering if there is a way to do this kind of job in R. 但是我想知道是否有一种方法可以在R中完成这种工作。

And yes, I'm newbie to R world. 是的,我是R world的新手。

EDIT: 编辑:

This is my desired output: 这是我想要的输出:

       GO.ID Annotated Significant Expected P-value                   Term Ontology           Gene_ID
1 GO:0000049         7           0     0.25  1.0000           tRNA binding       MF           Solyc02g090860.2,Solyc03g119280.2,Solyc05g056260.2,Solyc06g048610.2,Solyc07g008950.2,Solyc08g015960.2,Solyc10g007060.2
2 GO:0000062         4           0     0.14  1.0000 fatty-acyl-CoA binding       MF           Solyc01g099350.2,Solyc03g082910.2,Solyc04g078090.2,Solyc08g075690.2 

如果df是你的data.frame和lst您的列表,你可以这样做:

transform(df, Gene_ID=sapply(lst, paste0, collapse=',')[GO.ID])

(I apologise for using dplyr here. All of this can be done using built-in R functions but I don't remember the last time I used them) (我很抱歉在这里使用dplyr 。所有这些操作都可以使用内置的R函数完成,但我不记得上次使用它们了)

library(dplyr)
library(tidyr)

# sample data
l <- list("GO.0000049" = c(1,2,3), "GO:0000062" = c(4,5,6))
df <- data.frame(GO.ID = c("GO.0000049", "GO:0000062"), Annotated = c(7,4), stringsAsFactors = F)

# actual magic
result <- gather(as_data_frame(lapply(l, function(x) paste(x, collapse=","))), "GO.ID", "Gene_ID") %>% inner_join(df)

And your result would be: 您的result将是:

Source: local data frame [2 x 3]

       GO.ID Gene_ID Annotated
1 GO.0000049   1,2,3         7
2 GO:0000062   4,5,6         4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM