我有两个任意的人A和B之间的对话记录。

c1 <- "Person A: blabla...something Person B: blabla something else Person A: OK blabla"
c2 <- "Person A: again blabla Person B: blabla something else Person A: thanks blabla"


数据框如下所示:

df <- data.frame(id = rbind(123, 345), conversation = rbind(c1, c2))

df

    id                                                                     conversation
c1 123 Person A: blabla...something Person B: blabla something else Person A: OK blabla
c2 345   Person A: again blabla Person B: blabla something else Person A: thanks blabla


现在,我只想提取人A的一部分并将其放入数据框中。结果应为:

   id                     person_A
1 123 blabla...something OK blabla
2 345   again blabla thanks blabla

最佳答案

我非常乐于以一种使您能够访问所有数据(也包括B人的论述)的方式来解决此类问题。我喜欢提迪尔的extract这种列拆分方法。我曾经使用do.call(rbind, strsplit()))方法,但是喜欢extract方法的简洁程度。

c1 <- "Person A: blabla...something Person B: blabla something else Person A: OK blabla"
c2 <- "Person A: again blabla Person B: blabla something else Person A: thanks blabla"
c3 <- "Person A: again blabla Person B: blabla something else"
df <- data.frame(id = rbind(123, 345, 567), conversation = rbind(c1, c2, c3))


if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, tidyr)

conv <- strsplit(as.character(df[["conversation"]]), "\\s+(?=Person\\s)", perl=TRUE)

df2 <- df[rep(1:nrow(df), sapply(conv, length)), ,drop=FALSE]
rownames(df2) <- NULL
df2[["conversation"]] <- unlist(conv)

df2 %>%
    extract(conversation, c("Person", "Conversation"), "([^:]+):\\s+(.+)")

##    id   Person          Conversation
## 1 123 Person A    blabla...something
## 2 123 Person B blabla something else
## 3 123 Person A             OK blabla
## 4 345 Person A          again blabla
## 5 345 Person B blabla something else
## 6 345 Person A         thanks blabla
## 7 567 Person A          again blabla
## 8 567 Person B blabla something else


df2 %>%
    extract(conversation, c("Person", "Conversation"), "([^:]+):\\s+(.+)") %>%
    filter(Person == "Person A")

##    id   Person       Conversation
## 1 123 Person A blabla...something
## 2 123 Person A          OK blabla
## 3 345 Person A       again blabla
## 4 345 Person A      thanks blabla
## 5 567 Person A       again blabla


或按照所需输出中的显示折叠它们:

df2 %>%
    extract(conversation, c("Person", "Conversation"), "([^:]+):\\s+(.+)") %>%
    filter(Person == "Person A") %>%
    group_by(id) %>%
    select(-Person) %>%
    summarise(Person_A =paste(Conversation, collapse=" "))

##    id                     Person_A
## 1 123 blabla...something OK blabla
## 2 345   again blabla thanks blabla
## 3 567                 again blabla


编辑:实际上,我怀疑您的数据具有真实姓名,例如“ john Smith”与“ Person A”。如果是这种情况,则此初始正则表达式拆分将捕获使用大写字母和冒号的姓氏和名字:

c1 <- "Greg Smith: blabla...something Sue Williams: blabla something else Greg Smith: OK blabla"
c2 <- "Greg Smith: again blabla Sue Williams: blabla something else Greg Smith: thanks blabla"
c3 <- "Greg Smith: again blabla Sue Williams: blabla something else"
df <- data.frame(id = rbind(123, 345, 567), conversation = rbind(c1, c2, c3))r


conv <- strsplit(as.character(df[["conversation"]]), "\\s+(?=([A-Z][a-z]+\\s+[A-Z][a-z]+:))", perl=TRUE)

df2 <- df[rep(1:nrow(df), sapply(conv, length)), ,drop=FALSE]
rownames(df2) <- NULL
df2[["conversation"]] <- unlist(conv)

df2 %>%
    extract(conversation, c("Person", "Conversation"), "([^:]+):\\s+(.+)")

##    id       Person          Conversation
## 1 123   Greg Smith    blabla...something
## 2 123 Sue Williams blabla something else
## 3 123   Greg Smith             OK blabla
## 4 345   Greg Smith          again blabla
## 5 345 Sue Williams blabla something else
## 6 345   Greg Smith         thanks blabla
## 7 567   Greg Smith          again blabla
## 8 567 Sue Williams blabla something else

09-06 07:59