jeudi 14 juin 2018

Sample from a matrix and split sampled and non sampled values in R

I want to sample 'n' rows from a matrix:

data <- matrix(data = 1:12, nrow = 4, ncol = 3)

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

I use the following code :

selection <-sample(nrow(data),size = 2, replace = FALSE)
data[selection,]

     [,1] [,2] [,3]
[1,]    4    8   12
[2,]    3    7   11

Is there a way I could return a matrix containing only the rows that haven't been sampled? In this case:

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10

Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire