如何将字符串列表拆分为记录?

时间:2014-02-12 18:11:56

标签: r split

来自记录

的文件中的readLines
record:1
...
end

junk

record:2
...
end

more junk

所以记录的位置是

beg.pos <- grep("^record:[0-9]*",l)
end.pos <- grep("^end",l)

那么,我如何将split向量l放入字符串向量列表中:

list(c("record:1",...,"end"),
     c("record:2",...,"end"))

3 个答案:

答案 0 :(得分:1)

mapply(function(b, e) c(l[b:e]), beg.pos, end.pos, SIMPLIFY=FALSE)
# [[1]]
# [1] "record:1" "..."      "end"     
# 
# [[2]]
# [1] "record:2" "..."      "end"

答案 1 :(得分:0)

尝试以下方法:

# Your data:
l = c("record:1", "...", "end", "", "junk", "", "record:2", "...", "end", "", "more junk")    
beg.pos <- grep("^record:[0-9]*",l)
end.pos <- grep("^end",l)

# Splitting:
l2 = list()
for (i in 1:length(beg.pos)) {
  l2 = c(l2, list(l[beg.pos[i]:end.pos[i]]))
}

输出:

> l2
[[1]]
[1] "record:1" "..."      "end"     
[[2]]
[1] "record:2" "..."      "end"     

答案 2 :(得分:0)

我无法对您的数据进行真正的测试,但这样的事情应该有效:

beg.pos <- grep("^record:[0-9]*",l)
end.pos <- grep("^end",l)

listing <- list()

# Create a listing of sequences
for(i in 1:length(beg.pos)) { 
   listing[[i]] <- beg.pos[i]:end.pos[i]
}

# Return a list of subsets
lapply(listing, function(x) l[x])