I'm trying to import into a data frame a conversation with the following structure:
conversation<-data.frame(
uniquerow=c("01/08/2015 2:49:49 pm: Person 1: Hello",
"01/08/2015 2:51:49 pm: Person 2: Nice to meet you",
"01/08/2015 2:59:19 pm: Person 1: Same here"))
This structure would make it relatively easy to parse the date, time, person and message. But there are a few instances where the message carries a newline, and thus the data frame is miss-structured, like such:
conversation_errors<-data.frame(
uniquerow=c("01/08/2015 2:49:49 pm: Person 1: Hello",
"01/08/2015 2:51:49 pm: Person 2: Nice to meet you",
"01/08/2015 2:59:19 pm: Person 1: Same here, let me tell you a haiku: ",
"lend me your arms,",
"fast as thunderbolts,",
"for a pillow on my journey."))
How would you go about merging these instances? Is there any package i'm not aware of?
The desired function would simply recognise the missing structure and "merge" with the preceding row, such that I would get:
conversation_fixed<-data.frame(
uniquerow=c("01/08/2015 2:49:49 pm: Person 1: Hello",
"01/08/2015 2:51:49 pm: Person 2: Nice to meet you",
"01/08/2015 2:59:19 pm: Person 1: Same here, let me tell you a haiku: lend me your arms, fast as thunderbolts, for a pillow on my journey."))
Any thoughts?
Assuming that you can correctly identify the properly structured rows using the timestamp (represented below in properDataRegex), then this will do it:
mydata <- c("01/08/2015 2:49:49 pm: Person 1: Hello",
"01/08/2015 2:51:49 pm: Person 2: Nice to meet you",
"01/08/2015 2:59:19 pm: Person 1: Same here, let me tell you a haiku: ",
"lend me your arms,",
"fast as thunderbolts,",
"for a pillow on my journey.",
"07/07/2015 3:29:00 pm: Person 1: This is not the most efficient method",
"but it will get the job done.")
properDataRegex <- "^\\d{2}/\\d{2}/\\d{4}\\s"
improperDataBool <- !grepl(properDataRegex, mydata)
while (sum(improperDataBool)) {
mergeWPrevIndex <- which(c(FALSE, !improperDataBool[-length(improperDataBool)]) &
improperDataBool)
mydata[mergeWPrevIndex - 1] <-
paste(mydata[mergeWPrevIndex - 1], mydata[mergeWPrevIndex])
mydata <- mydata[-mergeWPrevIndex]
improperDataBool <- !grepl(properDataRegex, mydata)
}
mydata
## [1] "01/08/2015 2:49:49 pm: Person 1: Hello"
## [2] "01/08/2015 2:51:49 pm: Person 2: Nice to meet you"
## [3] "01/08/2015 2:59:19 pm: Person 1: Same here, let me tell you a haiku: lend me your arms, fast as thunderbolts, for a pillow on my journey."
## [4] "07/07/2015 3:29:00 pm: Person 1: This is not the most efficient method but it will get the job done."
Here, mydata is a character vector but of course it's now trivial to make into a data.frame as you had in the question, or to parse it using read.table() or read.fwf().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With