Suppose I have a 10 x 10 matrix. I want to randomly choose 2 numbers from each column and take the square of the difference of these numbers. I wrote the R code for that and I get 10 values, but I wish to repeat this, say, 100 times, in which case I need to get 100*10=1000 numbers. How could I do that?
x <- rnorm(100)
m <- 10
n <- 10
X <- matrix(x,m,n)
for (i in 1:m ) {
y <- sample(X[,i],2,rep=F)
q2[i] <- (y[1]-y[2])^2
}
?replicate- Davide Passarettiforloop as a function and usereplicateas @Davide Passaretti suggested - nrussellreplicate(100, sapply(data.frame(X), function(y) diff(sample(y, 2))^2))will work - David Arenburg