-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
recapFactorFlip equivalent #390
Comments
yeah, @nutterb has done some cool things in redcapAPI. Here's the code for his function: https://github.com/nutterb/redcapAPI/blob/master/R/redcapFactorFlip.R. I haven't studied it too closely, but it looks like he gets the raw values & labels and makes them factors. Here are the parts that strike me as most relevant: https://github.com/nutterb/redcapAPI/search?q=redcapLabels Regarding how to replicate it in REDCapR, I'm guess it would start with Here's a proof of concept that I haven't tested. Suppose the relevant parts of the metadata dataset are: ds <-
tibble::tribble(
~field_name, ~select_choices_or_calculations,
"record_id", NA_character_,
"age" , NA_character_,
"gender" , "0, Female | 1, Male",
"race" , "1, American Indian/Alaska Native | 2, Asian | 3, Native Hawaiian or Other Pacific Islander | 4, Black or African American | 5, White | 6, Unknown / Not Reported",
"ethnicity", "0, Unknown / Not Reported | 1, NOT Hispanic or Latino | 2, Hispanic or Latino"
) Something like this would extract each level. Notice rematch2, which is one of my favorite & underappreciated packages, does the hard work. pattern <- "^(?<level>\\d+),(?<label>.+)$"
ds |>
dplyr::select(
field = field_name,
choice = select_choices_or_calculations
) |>
tidyr::drop_na(choice) |>
tidyr::separate_rows(
choice,
sep = " \\| "
) |>
rematch2::bind_re_match(
choice,
pattern
) Here's resulting dataset, which (I think) can be piped into a purrr function to apply the levels & labels to each factor variable.
Does that help? If there's enough interest, I'll put it into the package. I'd love fed back from anyone who is interested in this potential. @pbchase, you usually have an opinion? |
I also think being able to move between raw data and labels would be useful. We built the parse_labels <- function(string){
out <- string %>%
strsplit(" \\| |, ") %>% # split either by ' | ' or ', '
unlist() %>%
matrix(
ncol = 2,
byrow = TRUE,
dimnames = list(
c(), # row names
c("raw", "label")) # column names
) %>%
dplyr::as_tibble()
out
}
string <- "1, American Indian/Alaska Native | 2, Asian | 3, Native Hawaiian or Other Pacific Islander | 4, Black or African American | 5, White | 6, Unknown / Not Reported"
parse_labels(string)
#> # A tibble: 6 × 2
#> raw label
#> <chr> <chr>
#> 1 1 American Indian/Alaska Native
#> 2 2 Asian
#> 3 3 Native Hawaiian or Other Pacific Islander
#> 4 4 Black or African American
#> 5 5 White
#> 6 6 Unknown / Not Reported This function should work well in a One improvement I would suggest to your above code is not to filter on |
redcapAPI has the helpful
redcapFactorFlip
function. Does REDCapR have an equivalent? If no, how might I replicate the same thing?The text was updated successfully, but these errors were encountered: