這是一種迂迴的可能性,使用split
,grepl
和cumsum
。
一些樣本數據:
temp <- c("This is first line.", "This is second line.",
"\\delimiter\\new\\one", "This is third line.",
"This is fourth line.", "\\delimiter\\new\\one",
"This is fifth line")
# [1] "This is first line." "This is second line." "\\delimiter\\new\\one"
# [4] "This is third line." "This is fourth line." "\\delimiter\\new\\one"
# [7] "This is fifth line"
使用split
使用cumsum
上grepl
產生 「團」 之後:
temp1 <- split(temp, cumsum(grepl("delimiter", temp)))
temp1
# $`0`
# [1] "This is first line." "This is second line."
#
# $`1`
# [1] "\\delimiter\\new\\one" "This is third line." "This is fourth line."
#
# $`2`
# [1] "\\delimiter\\new\\one" "This is fifth line"
如果進一步清理需要,這裏有一個選項:
lapply(temp1, function(x) {
x[grep("delimiter", x)] <- NA
x[complete.cases(x)]
})
# $`0`
# [1] "This is first line." "This is second line."
#
# $`1`
# [1] "This is third line." "This is fourth line."
#
# $`2`
# [1] "This is fifth line"
在嵌入式換行符,列表或向量長度之一這個'character' '字符',還是您尚未閱讀的文本文件? – 2013-03-20 04:34:59
請修改您的問題以顯示您的數據的確切結構(或一些示例數據)。例如,粘貼'dput(head(yourdata))'的結果。目前尚不清楚新線如何確定。 – Ben 2013-03-20 04:36:07