mardi 18 septembre 2018

row random sampling in r

I want to sample 60 random rows 1000 times with the replace=TRUE and calculate the correlation coefficients between first and second columns in each sample.

I don't know how to sample row randomly, so I tried to sample 60 numbers in 1:60, and matched the row numbers. The row data is 60x2 matrix which is called data1.

My code is

k <- list()
data.sam <- list()

set.seed(1)

for (j in 1:60){
  for (i in 1:1000){
  k[[i]] <- sample(1:60, 60, replace = TRUE)
  }
  data.sam[[i]][j,] <- data1[k[[i]][j],]

  corr <- vector()
  corr[i] <- cor(data.sam[[i]][,1],data.sam[[i]][,2])
}

And the statement is showed:

Error in `*tmp*`[[i]] : subscript out of bounds




Aucun commentaire:

Enregistrer un commentaire