来自向量的采样循环

发布于 2024-12-15 00:36:53 字数 481 浏览 2 评论 0原文

我正在开发一个采样函数来进行随机化,以使日子变得更轻松:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚你发端 2024-12-22 00:36:53

这是你想要的吗?

> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1] 13 19  4 16 11  2  5 20  9 14 10  3  1  7  6  8 17 12 15 18 27 24 30 32 23 25 28 21 31 26 29 22 39 41 48 36 37 45 42 47 43 38 40 34 35
[46] 44 46 33 60 52 50 58 51 54 62 55 64 61 59 49 63 53 56 57 72 74 76 78 67 69 70 66 73 79 68 80 77 71 75 65

测试:

> pln <- 1:12
> pln
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

> bcap <- cumsum(c(4, 3, 2, 3))
> bcap
[1]  4  7  9 12

> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  4  2  3  1  6  5  7  8  9 12 11 10
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  4  2  3  1  6  5  7  9  8 10 12 11
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  2  3  1  4  7  6  5  8  9 11 10 12

Is this what you want?

> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1] 13 19  4 16 11  2  5 20  9 14 10  3  1  7  6  8 17 12 15 18 27 24 30 32 23 25 28 21 31 26 29 22 39 41 48 36 37 45 42 47 43 38 40 34 35
[46] 44 46 33 60 52 50 58 51 54 62 55 64 61 59 49 63 53 56 57 72 74 76 78 67 69 70 66 73 79 68 80 77 71 75 65

Testing:

> pln <- 1:12
> pln
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

> bcap <- cumsum(c(4, 3, 2, 3))
> bcap
[1]  4  7  9 12

> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  4  2  3  1  6  5  7  8  9 12 11 10
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  4  2  3  1  6  5  7  9  8 10 12 11
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
 [1]  2  3  1  4  7  6  5  8  9 11 10 12
世态炎凉 2024-12-22 00:36:53

您只需调用一次 mapply 即可完成此操作。您只需要一个包含 bcap 对象的 cumsum 调用内部内容的对象。

bvec <- c(20, 12, 16, 16, 16)
mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec))

一个小例子:

bvec <- c(2,1,3,1)
set.seed(21)
unlist(mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec)))
# [1] 2 1 3 4 5 6 7

You can do this with one call to mapply. You just need an object that contains what's inside the cumsum call of your bcap object.

bvec <- c(20, 12, 16, 16, 16)
mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec))

A small example:

bvec <- c(2,1,3,1)
set.seed(21)
unlist(mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec)))
# [1] 2 1 3 4 5 6 7
潦草背影 2024-12-22 00:36:53
library("plyr")

unlist(
  llply(
    mlply(
      data.frame(from=c(1,bcap[-length(bcap)]), to=bcap), 
      seq),
    sample),
  use.names = FALSE)

创建一个包含每个范围 from/to 的 data.frame,使用它来创建包含序列的列表,对每个列表进行采样,然后将它们组合在一起。

更新:

为我工作:

> library("plyr")
> bcap <- cumsum(c(4, 3, 2, 3))
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  4  2  3  1  7  4  5  6  9  7  8 12  9 11 10
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  3  1  2  4  5  6  4  7  9  7  8  9 12 10 11
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  2  3  4  1  6  5  4  7  8  9  7 11 10 12  9
library("plyr")

unlist(
  llply(
    mlply(
      data.frame(from=c(1,bcap[-length(bcap)]), to=bcap), 
      seq),
    sample),
  use.names = FALSE)

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:

> library("plyr")
> bcap <- cumsum(c(4, 3, 2, 3))
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  4  2  3  1  7  4  5  6  9  7  8 12  9 11 10
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  3  1  2  4  5  6  4  7  9  7  8  9 12 10 11
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
 [1]  2  3  4  1  6  5  4  7  8  9  7 11 10 12  9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文