install.packages("sampling") library(sampling) install.packages("data.table") library(data.table) set.seed(1) N <- 1e4 d <- data.table(fasciaEta = sample(1:5, N, T), sesso = rbinom(N, 1 , .5), spesaMensile = rnorm(N, 100, 6.7)) # Sort setkey(d, fasciaEta, sesso) # Population size by strata d[, .N, keyby = list(fasciaEta, sesso)] # Select sample set.seed(2) s <- data.table(strata(d, c("fasciaEta", "sesso"), size=rep(30, 10), "srswor")) ?strata dim(s) # Sample size by strata s[, .N, keyby = list(fasciaEta, sesso)] s <- data.table(strata(d, c("fasciaEta", "sesso"), size = round(c(967/n*300, 958/n*300, 1116/n*300, 928/n*300, 1026/n*300, 984/n*300, 1025/n*300, 1027/n*300, 1007/n*300, 962/n*300)),"srswor"))