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

时间:2021-08-14 21:36:53

I readLines from a file with records

我从带有记录的文件中读取了线条

record:1
...
end

junk

record:2
...
end

more junk

So the positions of records are

所以记录的位置是

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

So, how do I split the vector l into a list of vectors of strings:

那么,如何将向量l分割为字符串向量列表:

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

3 个解决方案

#1


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"

#2


0  

Try the following:

请尝试以下方法:

# 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]]))
}

Output:

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

#3


0  

I can't really test this on your data, but something like this ought to work:

我不能真正测试你的数据,但这样的事情应该工作:

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])

#1


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"

#2


0  

Try the following:

请尝试以下方法:

# 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]]))
}

Output:

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

#3


0  

I can't really test this on your data, but something like this ought to work:

我不能真正测试你的数据,但这样的事情应该工作:

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])