Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Created May 23, 2024 17:59
Show Gist options
  • Save bschneidr/680eae22a6328a242626cc36cd020d30 to your computer and use it in GitHub Desktop.
Save bschneidr/680eae22a6328a242626cc36cd020d30 to your computer and use it in GitHub Desktop.
Sampling groups with 'dplyr' + 'tidyr'
library(dplyr)
library(tidyr)
library(survey)
# Load example data
# 'apipop' has one row per school
data('api', package = 'survey')
# Draw a sample of school districts
apipop |>
nest(.by = c(dname, dnum)) |>
slice_sample(n = 10) |>
unnest(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment