取消列出dataframe列中的多个值,但跟踪行号

时间:2017-04-05 20:02:19

标签: r strsplit

我有一个数据框,其中包含一个包含多个值的列,这些值由以分号分隔的基因名称同义词组成:

score <- c("32.01","19.5","18.0")
symbol <- c("30 kDa adipocyte complemen related protein","AAT1","Cachectin")
synonym <- c("30 kDa adipocyte complemen related protein; 30 kDa adipocyte complement-related protein; ACDC; ACRP30; ADIPOQ; APM-1; APM1; Adipocyte C1Q and collagen domain containing","AAT1; AAT1; ALT-1; ALT1; Alanine aminotransferase; Alanine aminotransferase 1; GPT 1; GPT1; Glutamate pyruvate transaminase; Glutamic--alanine transaminase 1; Glutamic--pyruvic transaminase 1","Cachectin; TNF alpha; TNF-a; TNFA; TNFSF-2; TNFSF2; TNFalpha; Tumor necrosis factor; Tumor necrosis factor ligand superfamily member 2; Tumor necrosis factor precursor; tumor necrosis factor alpha")
df <- data.frame(score, symbol, synonym, stringsAsFactors=FALSE)

这是数据挖掘的原始输出。我将数据中的官方基因符号映射到Entrez ID。符号列通常不包含基因符号,因此我必须提取所有同义词(通常,列表中有正式符号)。我想要跟踪行号的目标是,一旦我将所有符号映射到Entrez ID,我就可以识别那些没有映射的行。

我目前正在使用strsplit和unlist来解析同义词,但我忘记了每个同义词来自哪一行:

tmp <- data.frame(unlist(strsplit(as.character(df$synonym), "; ")))

我想要的是这样的东西:

originalRow <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3)
cbind(tmp, originalRow)

   synonym                                           originalRow 
1   30 kDa adipocyte complemen related protein           1
2   30 kDa adipocyte complement-related protein          1
3   ACDC                                                 1
4   ACRP30                                               1
5   ADIPOQ                                               1
6   APM-1                                                1
7   APM1                                                 1
8   Adipocyte C1Q and collagen domain containing         1
9   AAT1                                                 2
10   AAT1                                                2
11   ALT-1                                               2
12   ALT1                                                2
13   Alanine aminotransferase                            2
14   Alanine aminotransferase 1                          2
15   GPT 1                                               2
16   GPT1                                                2
17   Glutamate pyruvate transaminase                     2
18   Glutamic--alanine transaminase 1                    2
19   Glutamic--pyruvic transaminase 1                    2
20   Cachectin                                           3
21   TNF alpha                                           3
22   TNF-a                                               3
23   TNFA                                                3
24   TNFSF-2                                             3
25   TNFSF2                                              3
26   TNFalpha                                            3
27   Tumor necrosis factor                               3
28   Tumor necrosis factor ligand superfamily member 2   3
29   Tumor necrosis factor precursor                     3
30   tumor necrosis factor alpha                         3

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:1)

在这里,您可以拆分每一行,将行的值与行号组合到一个data.frame中,然后将所有这些data.frames绑定在一起。你可以用

做到这一点
do.call("rbind", Map(data.frame, 
    synonym=strsplit(as.character(df$synonym), ";"), 
    origRow=seq_along(df$synonym))
)

答案 1 :(得分:0)

另一种方法是将同义词存储在列表中,然后可以迭代以与符号数组进行比较。在单个列表元素中工作不需要跟踪原始行号。这也将修剪空白以进行比较。

lst <- lapply(synonym, function(x) trimws(unlist(strsplit(x, ";"))))
lapply(lst, setdiff, symbol)  # return values not in symbol array

[[1]]
[1] "30 kDa adipocyte complement-related protein"  "ACDC"                                        
[3] "ACRP30"                                       "ADIPOQ"                                      
[5] "APM-1"                                        "APM1"                                        
[7] "Adipocyte C1Q and collagen domain containing"

[[2]]
[1] "ALT-1"                            "ALT1"                             "Alanine aminotransferase"        
[4] "Alanine aminotransferase 1"       "GPT 1"                            "GPT1"                            
[7] "Glutamate pyruvate transaminase"  "Glutamic--alanine transaminase 1" "Glutamic--pyruvic transaminase 1"

[[3]]
[1] "TNF alpha"                                         "TNF-a"                                            
[3] "TNFA"                                              "TNFSF-2"                                          
[5] "TNFSF2"                                            "TNFalpha"                                         
[7] "Tumor necrosis factor"                             "Tumor necrosis factor ligand superfamily member 2"
[9] "Tumor necrosis factor precursor"                   "tumor necrosis factor alpha"