How to find a vector length of 'n' values, where each value is a mean of 'm' random elements of data in R?
How 开发者_开发问答to find a vector length of 'n' values, where each value is a mean of 'm' random elements of data in R?
Code below is for sampling with replacement.
random_nm<- function(n,m,v){
x <- sample(seq(length(v)),n*m,replace=TRUE)
rowMeans(matrix(v[x],nrow=n,ncol=m))
}
dat <- rnorm(100)
random_nm(5,20,dat)
[1] 0.05149423 -0.16239828 0.45485354 -0.10758954 -0.13597127
If you want samples without replacement first line of function random_rn has to be changed.
精彩评论