Find the original strings from the results of str_extract_all

Viewed 66

I'm still young to coding and cannot figure out better functions or results to some tasks by myself very often. I have a question on tracking the original string after using str_extract_all for a specific pattern.

Here is an example data called "fruit".

index Fruit
1 apple
2 banana
3 strawberry
4 pineapple
5 bell pepper

I used str_extract_all(fruit, "(.)\\1") to extract duplicated consonants, and get "pp", "rr", "pp", "ll", "pp".

Also tracked the original string (of those extracted results) by str_subset(fruit, "(.)\\1"). Here's what I get.

index fruit
1 apple
2 strawberry
3 pineapple
4 bell pepper

However, I want to know where "each" extracted result is from. Therefore, using str_subset cannot capture those results which are from the same string. The following dataframe is what I expect to gain.

index fruit pattern
1 apple pp
2 strawberry rr
3 pineapple pp
4 bell pepper ll
4 bell pepper pp

I'm not sure if I explain my question clearly. Any feedbacks and ideas will be appreciate.

1 Answers

Your code already did what you want. You just need to create an extra column to store the output of str_extract_all, like the following:

Since str_extract_all() returns a list, we'll need to unnest the list to become rows.

The final line of the code is to create a consecutive index (since "banana" is gone, index 2 will also be gone).

library(tidyverse)

fruit %>% 
  mutate(pattern = str_extract_all(Fruit, "(.)\\1")) %>% 
  unnest(pattern) %>%
  mutate(index = as.numeric(as.factor(index)))

# A tibble: 5 × 3
  index Fruit       pattern
  <dbl> <chr>       <chr>  
1     1 apple       pp     
2     2 strawberry  rr     
3     3 pineapple   pp     
4     4 bell pepper ll     
5     4 bell pepper pp   
Related