Extract multiple paragraphs from text string after they match a pattern with R

Viewed 48

I have a data frame with one column text as strings of interview transcripts between two people.

The format is firstname lastname m:ss then a line break, a paragraph of text, a line break, then firstname lastname m:ss a line break and a paragraph of text as below.

John Smith 2:03
Is this great?
Joe Blogs 2:24
Yes it is.
John Smith 2:35
Is it the greatest of all time?
Joe Blogs 2:47
Clearly. 

I want to make 2 new strings to go in two new columns in my df. One for each person containing the paragraphs of text after their names strung together.

The interviewer in each of the text files is one of two people so I could match their name to assign them to an ‘interviewer”. I also have the interviewer name in an interviewer column of the df already.

Interviewer names: John Smith, Jane Doe

Desired Output:

interviewer subject
Is this great? Is it the greatest of all time? Yes it is. Clearly.

I'm not sure the best way to achieve what I am after. I've used tidyr extract to the interviewer, date etc. from the file names to put them into columns in my dataframe, but am not experienced enough to know how to use it to split the text like this - or if there's a better way to do it.

1 Answers

Update: A string independent version:

df %>%
  filter(row_number() %% 2 == 0) %>% 
  group_by(x = rep(c("subject", "interviewer"), length.out = n())) %>% 
  mutate(text = paste(text, collapse = " ")) %>% 
  filter(row_number() %% 2 == 0) %>% 
  pivot_wider(names_from = x, values_from = text) 
 subject                                        interviewer        
  <chr>                                          <chr>              
1 Is this great? Is it the greatest of all time? Yes it is. Clearly.

First answer: We could do it this way:

library(dplyr)
library(tidyr)
library(stringr)

df %>% 
  filter(str_detect(text, '\\.|\\?')) %>% 
  mutate(x = ifelse(str_detect(text, '\\.'), "subject", "interviewer")) %>% 
  arrange(x) %>% 
  group_by(x) %>%
  summarise(text = paste(text, collapse = " ")) %>% 
  pivot_wider(names_from = x, values_from = text)
 interviewer                                    subject            
  <chr>                                          <chr>              
1 Is this great? Is it the greatest of all time? Yes it is. Clearly.
Related