For example, if my data frame is as follows-
Column_id Column_text
123 A: Hello
B: Hi, How are you?
A: I am good. What about you?
B: Good,
thanks
How can I get the output in the following format?
Column_id Column_text Text_from_A Text_from_B 123 (all the text) Hello I am good. What about you Hi, how are you? good,thanks
I basically need A text and B text in two different columns which should be added to my original data frame.
Any help is appreciated.
Thanks.
Updated posted: This is how it looks with dput
structure(list(Id = 12:13, Body_text = structure(1:2, .Label = c("Chat started: Thursday, December 5, 2019, 13:15:09
Chat Origin sales department
Agent John
John: Hi, How may I help you today?
John: Please hold on for a moment.
Customer: Hi, I have a question regarding a car model Price.
, John: Yes, may I know which model it is?
Customer: Yes, model abc.
John:The price of this is $$$$$.
Customer: Thank you
John:Have a good day.
",
"Chat started: wednesday, December 4, 2019, 13:15:09
Chat Origin sales department
Agent Mike
Mike: Hi, How may I help you today?
Customer: Hi, I would like to know the price of ABC car model
John:The price of this is $$$$$.
Customer: Thank you
Mike:Have a good day!
Customer:Thanks, you too!
"
), class = "factor")), .Names = c("Id", "Body_text"), row.names = 1:2, class = "data.frame")
I am trying to write the ouput to a csv file but not getting ... don't know where I am going wrong. Any help is appreciated. Thanks!
library(dplyr)
library(tidyr)
library(stringr)
library(bindrcpp)
df2<-read.csv("filepath.csv", header=TRUE, sep=",")
df2 %>%
mutate(Body_text = sub("^.*Agent\\s\\w+", "", Body_text)) %>%
separate_rows(Body_text, sep="\\s(?=\\w+:)") %>%
separate(Body_text, into = c("Text_from", "value"), sep=":\\s?") %>%
na.omit %>%
group_by(Id, newgrp = str_c('Text_from_',
replace(Text_from, Text_from != "Customer", "Agent"))) %>%
mutate(rn = row_number()) %>%
pivot_wider(names_from = newgrp, values_from = value) %>%
group_by(Id) %>%
result<-summarise(AgentName = first(Text_from),
Text_from_Agent = str_c(na.omit(Text_from_Agent), collapse=''),
Text_from_Customer = str_c(na.omit(Text_from_Customer), collapse = ''))%>%
write.csv(result,"filepathresult.csv")