R: Sample a vector with replacement multiple times

前端 未结 3 1180
时光取名叫无心
时光取名叫无心 2020-12-31 08:55

I\'d like to sample a vector x of length 7 with replacement and sample that vector 10 separate times. I\'ve tried the something like the following but can\'t get the result

3条回答
  •  别那么骄傲
    2020-12-31 09:36

    Looks like you got a suitable answer, but here's an approach that's similar to your first attempt. The difference is that we define samp with the appropriate dimensions, and then iteratively index into that object and fill it one row at a time:

    samp <- matrix(NA, ncol = 7, nrow = 10)
    for(i in 1:10){
      samp[i,] <- sample(x, size = length(x), replace = T)
    }
    

提交回复
热议问题