See the FAQ: How to do a minimal reproducible example reprex
for beginners. It's great that you included most of what's required. Preferably it turns out to be something that can be cut-and-pasted to give the exact error message.
Here's my reprex
. All I changed is data
to dat
because data()
, like df()
and others, are conflicting names. Sometimes a user assigned name takes precedence and other times it is the function. Whenever the cannot subset a closure message it's usually because of the naming issues.
library(sampling)
dat <- rbind(matrix(rep("nc", 165), 165, 1, byrow = TRUE), matrix(rep("sc", 70), 70, 1, byrow = TRUE))
dat <- cbind.data.frame(
dat, c(rep(1, 100), rep(2, 50), rep(3, 15), rep(1, 30), rep(2, 40)),
1000 * runif(235)
)
names(dat) <- c("state", "region", "income")
s <- strata(dat,c("region","state"),size=c(10,5,10,4,6), method="srswor")
head(s)
#> region state ID_unit Prob Stratum
#> 6 1 nc 6 0.1 1
#> 14 1 nc 14 0.1 1
#> 29 1 nc 29 0.1 1
#> 31 1 nc 31 0.1 1
#> 65 1 nc 65 0.1 1
#> 68 1 nc 68 0.1 1