在 R 中将向量拆分为不相等的块 [英] Split a vector into unequal chunks in R

查看:21
本文介绍了在 R 中将向量拆分为不相等的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和这里有同样的问题,除了我想要用另一个向量指定可变分割长度.所以,像这样:

I have the same question as here, except I want to specify the variable split lengths with another vector. So, something like this:

example.data<-paste("ex",1:10,sep="")
example.data
 [1] "ex1"  "ex2"  "ex3"  "ex4"  "ex5"  "ex6"  "ex7"  "ex8"  "ex9"  "ex10"
split.lens <- c(4,2,1,3)

应该给我以下列表:

result.list
[[1]]
[1] "ex1" "ex2" "ex3" "ex4"

[[2]]
[1] "ex5" "ex6"

[[3]]
[1] "ex7"

[[4]]
[1] "ex8"  "ex9"  "ex10"

我想不出使用 split 做到这一点的最佳方法.有什么想法吗?

I can't figure out the best way to do this with split. Any ideas?

谢谢!

推荐答案

split(example.data, rep(1:4, c(4,2,1,3)))

这篇关于在 R 中将向量拆分为不相等的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆