-1

I have few column names from which I need to get middle string. For example;

From this list

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

I need to get "Bcell" for each of the element.

Any help to solve this is appreciated.

Jaap
  • 81,064
  • 34
  • 182
  • 193
user44552
  • 153
  • 1
  • 10

1 Answers1

2

Try this:

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]

output-

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

If you specifically want Bcell then-

final_res <- result[result=="Bcell"]
Rushabh Patel
  • 2,672
  • 13
  • 34