Shahzaib Ali
Shahzaib Ali

Reputation: 13

How to use str_extract_all in R in data frame without returning a list?

I have been trying to extract multiple patterns in a sequence in a data frame of each row and returning those patterns in a new column. But the problem is i get a list if i use str_extract_all and i don't how to unlist.

I have been trying to use the code at the bottom. The unnest does not work either neither does unlist in mutate function.

enter image description here

dc <- z %>%
   mutate(sequence_match = str_extract_all(z$Sequence,
                                           c("R..S", "R..T", "R..Y")))

Upvotes: 0

Views: 1373

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389165

You can return one comma-separated string of values.

library(dplyr)
library(stringr)

dc = z %>% 
  mutate(sequence_match = sapply(str_extract_all(Sequence, 
                                 c("R..S", "R..T", "R..Y"), toString)))

Upvotes: 1

Related Questions