Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Created February 19, 2020 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcdlucas/9994192dbc383908283e541236f486f9 to your computer and use it in GitHub Desktop.
Save timcdlucas/9994192dbc383908283e541236f486f9 to your computer and use it in GitHub Desktop.
set.seed(17022020)
# "inside" people and countries
names_in <- c('lisa', 'suzanne', 'rohan')
countries_in <- c('germany', 'austria-hungary', 'italy', 'france')
# "outside"
names_out <- c('tim', 'jen')
countries_out <- c('russia', 'Turkey', 'england')
# New people
names_new <- c('susan', 'tasmin')
# Sample in and out.
sample_in <- cbind(names_in, sample(countries_in, length(names_in)))
sample_out <- cbind(names_out, sample(countries_out, length(names_out)))
# Then sample new from remaining countries.
unsel <- c(countries_in[!(countries_in %in% sample_in[, 2])],
countries_out[!(countries_out %in% sample_out[, 2])])
)
sample_new <- cbind(names_new, sample(unsel))
rbind(sample_in, sample_out, sample_new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment