How do I split a line via a delimiter?

Viewed 41

I have a dataframe.

[1]  Question  1. What is your name? Question  2. Where do you live? Question  3. ~~~
[2]  Question  1. What are your hobbies? Question  2. What is your school? Question  3. ~~~

I want to split the lines with "question " as the delimiter. Can I solve this?

Result :

[1] Question  1. What is your name?
[2] Question  2. Where do you live?
[3] Question  3. ~~~
[4] Question  1. What are your hobbies?
[5] Question  2. What is your school?
[6] Question  3. ~~~
2 Answers

Using strsplit and lookarounds, trimws afterwards.

strsplit(x, '(?<=\\s)(?=Question)', perl=TRUE) |> lapply(trimws)
# [[1]]
# [1] "Question  1. What is your name?" "Question  2. Where do you live?" "Question  3.  How old are you?" 
# 
# [[2]]
# [1] "Question  1. What are your hobbies?"  "Question  2. What is your school?"   
# [3] "Question  3.  What are your hobbies?"

Data:

x <- c('Question  1. What is your name? Question  2. Where do you live? Question  3.  How old are you?',
       'Question  1. What are your hobbies? Question  2. What is your school? Question  3.  What are your hobbies?')

Here's a one-line solution using stringr::str_split. Use unlist to unlist.

str_split(x, "(?<=.)(?=Question)")

[[1]]
[1] "Question  1. What is your name? " "Question  2. Where do you live? "
[3] "Question  3.  ~~~~"              

[[2]]
[1] "Question  1. What are your hobbies? " "Question  2. What is your school? "  
[3] "Question  3.  ~~~~" 

data

x <- c('Question  1. What is your name? Question  2. Where do you live? Question  3.  ~~~~',
       'Question  1. What are your hobbies? Question  2. What is your school? Question  3.  ~~~~')
Related