Random iteration loop in R

I would like to make a for loop that cycles through numbers 1-length(Matrix$Index)randomly. Please note that each room can only be visited once.

How can i achieve this?

+4
source share
2 answers

for (i in sample(c(1:length(Matrix$Index)))) will achieve this.

You can get different patterns by changing the seed with set.seed(). Setting a certain amount of seeds per sample will allow reproducibility.

+3
source

I do not understand your question perfectly, but I think you are trying to reselect without replacing on your vector if you want to use the built-in R function sample()and then:

n<-10
x<-rnorm(n)
resampled<-sample(x,length(x),replace=F)

, (x) N (0,1). - , - :

resampled<-numeric(n)

for(i in 1:n){
              a<-sample(1:n,1)
              resampled[i]<-x[a]
              x<-x[-a]
              n<-n-1
              }
0

Source: https://habr.com/ru/post/1667743/


All Articles