将列表转换为列

时间:2018-10-15 08:57:03

标签: r list

我的列表包含由/分隔的元素,其中只有第一个元素包含名称miR-。我想将miR粘贴到/符号之后的所有元素上,并将列表转换为列表ID(2869、10527等)和列表元素在侧面的表或数据框。并排。

列表

$`2869`
[1] "miR-539/539-5p"    "miR-135ab/135a-5p"

$`10527`
 [1] "miR-128/128ab"                                                                            
 [2] "miR-129-5p/129ab-5p"                                                                      
 [3] "miR-142-3p"                                                                               
 [4] "miR-145"                                                                                  
 [5] "miR-153"                                                                                  
 [6] "miR-140/140-5p/876-3p/1244"

输出:

2869  miR-539
2869  miR-539-5p
2869  miR-135ab
2869  miR-135a-5p
10527 miR-128
10527 miR-128ab
10527 miR-129-5p
10527 miR-129ab-5p
10527 miR-142-3p
...

Dput示例:

list(`6482` = c("miR-223", "miR-223", "miR-320abcd/4429", "miR-320abcd/4429", 
"miR-320abcd/4429", "miR-320abcd/4429", "miR-93/93a/105/106a/291a-3p/294/295/302abcde/372/373/428/519a/520be/520acd-3p/1378/1420ac", 
"miR-93/93a/105/106a/291a-3p/294/295/302abcde/372/373/428/519a/520be/520acd-3p/1378/1420ac", 
"miR-24/24ab/24-3p", "miR-24/24ab/24-3p", "miR-124/124ab/506", 
"miR-124/124ab/506", "miR-485-5p/1698/1703/1962", "miR-485-5p/1698/1703/1962", 
"miR-202-3p", "miR-202-3p", "miR-149", "miR-149", "miR-17/17-5p/20ab/20b-5p/93/106ab/427/518a-3p/519d", 
"miR-17/17-5p/20ab/20b-5p/93/106ab/427/518a-3p/519d", "let-7/98/4458/4500", 
"let-7/98/4458/4500", "miR-140/140-5p/876-3p/1244", "miR-140/140-5p/876-3p/1244"
), `4057` = c("miR-214/761/3619-5p", "miR-214/761/3619-5p"))

1 个答案:

答案 0 :(得分:0)

我们可以使用strsplit来很好地分割由\stack分隔的字符串,以将列表转换为数据框。最后,使用gsub为每个字符串加上"miR-"前缀。

df <- stack(lapply(lst, function(x) unlist(strsplit(x, split = "/"))))
df$values <- gsub("^(miR-)?(.*)", "miR-\\2", df$values)
df
#           values  ind
#1         miR-223 6482
#2         miR-223 6482
#3     miR-320abcd 6482
#4        miR-4429 6482
#5     miR-320abcd 6482
#6        miR-4429 6482
# ...