1

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

0

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

actually John (The OP) added that. I just tweaked it, otherwise the suggested edit probably would have been rejected.
0

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

Comments

0
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

2 Comments

thanks, for some reason - I am getting the following error:Error in do.call(flat, c(args, list(...))) : 'what' must be a character string or a function, you can test on the example in following reply..
@John: could you be using an out-of-date version of R and/or plyr?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.