拆分矢量以获得中间值

时间:2018-02-01 20:27:56

标签: r gsub strsplit

我有几个列名,我需要从中获取中间字符串。例如;

从此列表中

"RHC3934__Bcell__.7DEA7B","RHC3944__Bcell__.7DEA7B",  "RHC3962__Tcell__.C6E879", "RHC4003__Bcell__.7DEA7B", "RHC4005__Bcell__.7DEA7B", "RHC4007__Bcell__.7DEA7B"

我需要为每个元素获得" Bcell"

任何帮助解决这个问题都表示赞赏。

1 个答案:

答案 0 :(得分:2)

试试这个:

library(stringr)
a <- c("RHC3934__Bcell__.7DEA7B","RHC3944__Bcell__.7DEA7B",  "RHC3962__Tcell__.C6E879", "RHC4003__Bcell__.7DEA7B", "RHC4005__Bcell__.7DEA7B", "RHC4007__Bcell__.7DEA7B")

result <- str_match(a, "__(.*?)__")[,2]

输出 -

> result
[1] "Bcell" "Bcell" "Tcell" "Bcell" "Bcell" "Bcell"

如果您特别想要 Bcell ,那么 -

final_res <- result[result=="Bcell"]