fork(1) download
  1. test <- data.frame(
  2. Turn = c("Hi. I'm you an' you are me cos",
  3. "she'd've been so happy cos with all this stuff goin' on",
  4. "but we're in this together, because y' know things happens",
  5. "so you can't, cos well, ah because you know why!",
  6. "not now because it's too late!"), stringsAsFactors = F)
  7. rx <- "^\\s*(?:\\S+\\s+){0,3}(?:cos|because)\\b.*(*SKIP)(*F)|(?:\\S+[\\s,]+){4}\\b(cos|because)\\b"
  8. Turn <- test[grepl(rx, test$Turn, perl=TRUE),]
  9. split <- strsplit(Turn, "\\b(cos|because)\\b")
  10. Index <- sapply(split, function(x) lengths(strsplit(trimws(x[[1]]), "\\s+"))+1)
  11. test <- data.frame(Turn, Index, stringsAsFactors = F)
  12. test
Success #stdin #stdout 0.32s 39776KB
stdin
Standard input is empty
stdout
                                                        Turn Index
1                             Hi. I'm you an' you are me cos     8
2    she'd've been so happy cos with all this stuff goin' on     5
3 but we're in this together, because y' know things happens     6