Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个整数向量,例如c(1,2,0,3,4),我希望 0 作为分隔符并获取list(c(1,2), c(3,4)). 我可以使用任何库函数吗?
c(1,2,0,3,4)
list(c(1,2), c(3,4))
我知道没有库函数,但您可以编写自己的:
split.vec <- function(vec, sep = 0) { is.sep <- vec == sep split(vec[!is.sep], cumsum(is.sep)[!is.sep]) } split.vec(c(1,2,0,3,4), 0) # $`0` # [1] 1 2 # # $`1` # [1] 3 4