Skip to content

Instantly share code, notes, and snippets.

@simon-anders
Created November 18, 2025 10:08
Show Gist options
  • Select an option

  • Save simon-anders/525d750b09733e2c9276dbbb161e2e45 to your computer and use it in GitHub Desktop.

Select an option

Save simon-anders/525d750b09733e2c9276dbbb161e2e45 to your computer and use it in GitHub Desktop.
library( tidyverse )
library( haven )
left_join(
read_xpt("~/Downloads/DEMO_J.xpt"),
read_xpt("~/Downloads/BMX_J.xpt"),
by="SEQN", relationship="one-to-one" ) %>%
select( subjectId=SEQN, gender_code=RIAGENDR,
age=RIDAGEYR, ethnicity_code=RIDRETH3, height=BMXHT ) -> nhanes0
tibble(
code = c( 1, 2 ),
gender = c( "male", "female" )
) -> gender_codes
tibble(
code = c( 1, 2, 3, 4, 6, 7 ),
ethnicity = c( "Mexican", "Hispanic", "NH_White",
"NH_Black", "NH_Asian", "Other/Multi")
) -> ethnicity_codes
nhanes0 %>%
left_join( ethnicity_codes, by=c("ethnicity_code"="code"),
relationship="many-to-one") %>%
left_join( gender_codes, by=c("gender_code"="code"),
relationship="many-to-one") %>%
select( -gender_code, -ethnicity_code ) -> nhanes
nhanes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment