Reputation: 2666
I am performing a bootstrap analysis of community similarity values. I have a matrix of species counts, where the columns represent unique samples, and the rows unique species. Here is an example:
#generate a matrix with 20 columns, 30 rows, random values
set.seed(69) #for reproducibility.
otu <-matrix(rpois(20*30, lambda = 2), ncol=20)
I have two vectors that are environmental covariates associated with the unique samples. Therefore, each of these vectors is length 20.
v1 <- rnorm(20)
v2 <- rnorm(20)
I want to create a new species observation matrix that randomly samples the columns of the otu
matrix, with replacement. I can do this fairly easily.
#randomnly sample the columns of the otu matrix with replacement.
otu.boot <- otu[,sample(ncol(otu),size=ncol(otu),replace=TRUE)]
My question is, how can I sample vectors v1
and v2
in the same order as the randomly sampled with replacement matrix?
Upvotes: 0
Views: 378
Reputation: 2666
Following @lmo's suggestion in the comments:
#establish a sample order with replacement.
myOrder <- sample(ncol(otu), replace = T)
#go ahead and submsample both the otu matrix and vectors in that order.
otu.boot <- otu[,myOrder]
v1.boot <- v1[myOrder]
v2.boot <- v2[myOrder]
Upvotes: 0