Sample generation in R

2

I have a dataset with 200 observations. I generated a sample without replacement of size 100 with the following commands:

library(car)
require(car)
(amostra1= some(dados,n=100,replace=F))
write.xlsx(amostra1,"C:/Users/../Desktop/amostra1.xlsx")

My interest is also in non-sampled observations. The question is: Which command (s) in the R should I use to get the not sampled observations?

    
asked by anonymous 24.10.2018 / 22:39

1 answer

1

Considering how you are choosing amostra1 , the natural way to get the other data is either %in% and which or match . First I'm going to create a vector dados .

library(car)

set.seed(7437)    # Torna os resultados reprodutíveis
dados <- rnorm(200)

Now the choice of others.

amostra1 <- some(dados, n = 100, replace = FALSE)

i1 <- which(!dados %in% amostra1)
dados[i1]

i2 <- match(amostra1, dados)
dados[-i2]

identical(dados[i1], dados[-i2])
#[1] TRUE

If instead of directly sampling the dados sample index of the dados vector, simply deny this index to get the others.

j <- some(seq_along(dados), n = 100, replace = FALSE)

amostra1 <- dados[j]
outros1 <- dados[-j]
    
24.10.2018 / 23:29