2
votes

Is there any function can solve this kind of different-sized random resampling problem? For example, given a vector, data = c('a','a','b','c','d','e'). I want to randomly resample this vector into 3 groups with different sizes 1 ,3 ,2 respectively. Like

input:  samplefunc(data,size = c(1,3,2)) 
output: c('a')  c('a','d','e')  c('b','c')

I only found this "sample" function, but it is only for one size sample:

sample(x, size, replace = FALSE, prob = NULL)
size: a non-negative integer giving the number of items to choose.

Since I have to divide the data into many groups(not just 3), if there is an existed function can do that, it will be much easier without the for-loop.

1

1 Answers

3
votes

You can easily write your own function using, say, lapply, which would return a list of your samples:

samplefunc <- function(vec, size, ...) lapply(size, function(x) sample(vec, x, ...))

Usage would be as you imagined:

samplefunc(data, c(1, 3, 2))

As @thelatemail suggests, if you wanted to do sampling without replacement, you can try defining samplefunc as:

samplefunc <- function(vec, size) {
  temp <- split(vec, sample(rep(size, size)))
  temp[match(names(temp), as.character(size))]
}