从字符串集中提取第一个字符

时间:2019-01-29 10:45:50

标签: r

我有这个染色体编号注释列表。我将从每个字符串中提取第一个字符

[[1]]
[1] "KQ415812.1"

[[2]]
 [1] "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" 
[12] "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" "KQ415813.1" 


[[3]]
 [1] "KQ415814.1" "KQ415814.1" "KQ415814.1" "KQ415814.1" "KQ415814.1" "KQ415813.1"
[12] "KQ415814.1"

我会得到这个结果,因为每个字符串中的字符都是重复的,所以我只需要显示一次

[[1]]
[1] "KQ415812.1"

[[2]]
 [1] "KQ415813.1" 
[12] "KQ415813.1" 

[[3]]
 [1] "DKQ415814.1"
 [12] "DKQ415814.1"

有帮助吗?谢谢

2 个答案:

答案 0 :(得分:1)

purrr解决方案:

library(purrr)

yours_list %>%
  map(~.x[1])

答案 1 :(得分:0)

遍历listlapply),创建一个逻辑表达式以获取第一个元素

lapply(lst1, function(x) x[!duplicated(x)])