作者celestialgod (攸蓝)
看板R_Language
标题Re: [问题] 该如何分割
时间Sun Mar 16 22:13:16 2014
### 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)
1F:推 ryansun:谢谢您! 我再好好研究 03/17 10:12