我有一個資料框。
[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. ~~~
我想用“問題”作為分隔符來分割行。我可以解決這個問題嗎?
結果 :
[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. ~~~
uj5u.com熱心網友回復:
之后使用strsplit和環顧四周trimws。
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?"
資料:
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?')
uj5u.com熱心網友回復:
這是一個使用stringr::str_split.使用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. ~~~~"
資料
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. ~~~~')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/410300.html
標籤:
下一篇:通過正則運算式清理逗號分隔的串列
