如何将一个观察分解为多个观察?

How to break one observation down to several observation?

这是我的数据

title <- c("title1","title2")
text <- c("Mr.A.Speech1.\nMr.B.Speech2.\nMr.C.Speech3\n","Mrs.D.Speech4.\nMs.E.Speech5.\n")
df<- data.frame(title, text)

我希望每个演讲都作为标有标题的行:

     Title    Name.     Text
1.   title1   Mr.A      Speech1.
2.   title1   Mr.B      Speech2.
3.   title1   Mr.C      Speech3.
4.   title1   Mrs.D     Speech4.
5.   title1   Ms.E      Speech5.

我只能分解对文本的一种观察,例如分解

name <- unlist(str_extract_all(df$text,"\bM(?:rs?|s)\.\s[:upper:]{1,20}\s?c?'?-?[:upper:]{1,20}\s?o?f?\s?|The\sSPEAKER|The\sPRESIDING\s" ))
##breakdown speeches and remove content before the first name
pattern = "\bM(?:rs?|s)\.\s[:upper:]{1,20}\s?c?'?-?[:upper:]{1,20}\s?o?f?\s?|The\sSPEAKER|The\sPRESIDING\s"
df$text1 <- str_replace_all( df$text, pattern, "XXXX")
df$text2 <- gsub("^.*?XXXX","XXXX",df$text1)
dfa <- df[which(grepl("XXXX",df$text2)), ]

speech1 <- unlist(strsplit(df$text2, "XXXX"))
speech2 <- speech1[-1]
Text <- gsub("[\]", " ", speech2)

如何为每一行添加标题标签并为整列应用细分?谢谢!

不是最好的解决方案,但正则表达式不是我的强项

df %>% 
  separate_rows(text,sep = "\\n") %>%
  filter(text != "") %>% 
  separate(col = text,into = c("name","aux","text"),sep = "\.") %>% 
  mutate(name = paste(name,aux,sep = ".")) %>% 
  select(-aux)

# A tibble: 5 x 3
  title  name  text   
  <chr>  <chr> <chr>  
1 title1 Mr.A  Speech1
2 title1 Mr.B  Speech2
3 title1 Mr.C  Speech3
4 title2 Mrs.D Speech4
5 title2 Ms.E  Speech5