CHOP-CGTInformatics / REDCapTidieR

Makes it easy to read REDCap Projects into R
https://chop-cgtinformatics.github.io/REDCapTidieR/
Other
33 stars 8 forks source link

[FEATURE] Add a function to make the default REDCap demographic race variables (7 choices) into a single race variable while allowing for an 8th choice, Multiple Races. #192

Closed higgi13425 closed 5 months ago

higgi13425 commented 6 months ago

Feature Request Description

A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]; It would be great to have [...], etc.

Proposed Solution

A function called singlevar_race Add a function to make the default REDCap demographic race variables (7 choices, TRUE or FALSE) into a single race variable while allowing for an 8th choice, Multiple Races. The solution below also creates a race_num variable for folks who prefer numeric vars.

singlevar_race <- function(data) { data |> pivot_longer(cols = race___1:race___7, names_to = "race_num", names_prefix = "race___", values_to = "true") |> filter(true == 1) |> arrange(subject_id) |> mutate(lead = lead(subject_id)) |> mutate(race_num = case_when( subject_id == lead ~ "8", TRUE ~ race_num)) |> distinct(subject_id, .keep_all = TRUE) |> mutate(race = case_when( race_num == '1' ~ "American Indian or Alaska Native", race_num == '2' ~ "Asian", race_num == '3' ~ "Black or African American", race_num == '4' ~ "Native Hawaiian or Other Pacific Islander", race_num == '5' ~ "Not Reported", race_num == '6' ~ "Unknown", race_num == '7' ~ "White", race_num == '8' ~ "Multiple Races")) |> select(-true, -lead) }

Describe alternatives you've considered I think this works, assuming

  1. you have a standard variable named subject_id
  2. you use the standard REDCap demographics

Additional Context

Add any other context or screenshots about the feature request here.

Checklist

higgi13425 commented 5 months ago

Trying to make this prettier, and fix sbj_id to match REDCap standard demographics:

singlevar_race <- function(data) { data |> pivot_longer(cols = race1:race7,
names_to = "race_num",
names_prefix = "race___",
values_to = "true") |> filter(true == 1) |>
arrange(sbj_id) |>
mutate(lead = lead(sbj_id)) |>
mutate(race_num = case_when( sbj_id == lead ~ "8", TRUE ~ race_num)) |> distinct(sbj_id, .keep_all = TRUE) |>
mutate(race = case_when( race_num == '1' ~ "American Indian or Alaska Native", race_num == '2' ~ "Asian", race_num == '3' ~ "Black or African American", race_num == '4' ~ "Native Hawaiian or Other Pacific Islander", race_num == '5' ~ "Not Reported", race_num == '6' ~ "Unknown", race_num == '7' ~ "White", race_num == '8' ~ "Multiple Races")) |>
select(-true, -lead) }

higgi13425 commented 5 months ago

This will take a standard REDCap demographics table, and clean up from race1 to race7 to two columns of race_num and race, both character.

rsh52 commented 5 months ago

Closing this to open the new issue in #194

@higgi13425 feel free to add additional comments there if you like!