Re: [問題] 該如何分割
### 123.txt
comment: "it is nice and great"
comment: "it is nice"
comment: "it is great:"
### main.r
test=readLines("C:\\Users\\Administrator\\Desktop\\123.txt")
sapply(test, function(v) {
tmp = strsplit(v, " ")[[1]]
c(tmp[1], paste(tmp[2:length(tmp)], collapse = " "))})
Result:
[1,] "comment:" "comment:"
[2,] "\"it is nice and great\"" "\"it is nice\""
[1,] "comment:"
[2,] "\"it is great:\""
Note: 如果不要什麼,就用gsub取代成""就好
※ 引述《ryansun (sun)》之銘言:
: 請問我有一段話 形式大概如這樣
: comment: "it is nice and great"
: 想把comment與"it is nice and great"分別存在兩個變數裡
: 我試著用下列code做
: test=readLines("123.txt")
: strsplit(test, " ")
: 但結果是只要遇到空白就會切開
: [1] "comment:" "\"it" "is" "nice" "and" "great\""
: 有什麼方法是讀到第一個空白處才會分割 後面就不會繼續切下去
: 謝謝指教
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.238.86.11
※ 編輯: celestialgod 來自: 36.238.86.11 (03/17 00:06)
推
03/17 10:12, , 1F
03/17 10:12, 1F
討論串 (同標題文章)
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章