这是一种迂回的可能性,使用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