来自向量的采样循环
我正在开发一个采样函数来进行随机化,以使日子变得更轻松:
Question:
pln <- 1:80
bcap <- cumsum(c(20, 12, 16, 16, 16))
bcap
[1] 20 32 48 64 80
我想随机化 pln
这样 1:20, 21:32, 33:48, 49:64, 65:80 ,对于这个例子。对于不同的场景,这可能会有所不同。
newpln <- c(sample(1:20), sample(21:32), sample(33:48),
sample(49:64), sample(65:80))
我想创建一个通用函数,其中 bcap
的长度可以是任意数字,但是 pln
应该运行 1: max(bcap)
。
I am playing around to develop a sampling function to do randomization to make days easier:
Question:
pln <- 1:80
bcap <- cumsum(c(20, 12, 16, 16, 16))
bcap
[1] 20 32 48 64 80
I want to randomize pln
such that 1:20, 21:32, 33:48, 49:64, 65:80, for this example. This might vary for different scenarios.
newpln <- c(sample(1:20), sample(21:32), sample(33:48),
sample(49:64), sample(65:80))
I want create a general function where length of bcap
can be of any number, however the pln
should run 1: max(bcap)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是你想要的吗?
测试:
Is this what you want?
Testing:
您只需调用一次
mapply
即可完成此操作。您只需要一个包含bcap
对象的cumsum
调用内部内容的对象。一个小例子:
You can do this with one call to
mapply
. You just need an object that contains what's inside thecumsum
call of yourbcap
object.A small example:
创建一个包含每个范围 from/to 的 data.frame,使用它来创建包含序列的列表,对每个列表进行采样,然后将它们组合在一起。
更新:
为我工作:
Make a data.frame with each ranges from/to, use that to make a list with the sequences, sample each list, and then combine them together.
UPDATE:
worked for me: