Home > OS >  Need solution for recoding large number of variables
Need solution for recoding large number of variables

Time:11-27

I have a dataset with the English and Spanish version of a questionnaire. The questionnaires ask whether individuals have ever received a large number of different diagnoses. Each variable takes the form prev_dx_major_depression for the English data and prev_dx_major_depression_span for the Spanish data.

I would like to combine the two into a single variable. I am currently using the following code to achieve this purpose:

mutate(
    prev_dx_major_depression = if_else(prev_dx_major_depression == 1 | 
                                         prev_dx_major_depression_span == 1,
                                            1, 0
                                               ))

However, I know this is highly inefficient for such a large number of variables. My hunch is that I'll need to use some combination of mutate_at, recode, starts_with and ends_with. However, I am a bit stuck at this point and am not sure how to match up the corresponding variables together.

Here is some sample data:

sample_data <- 
  structure(
    list(
      id = 1:5,
      prev_dx_major_depression = c(0, 1, 1,
                                   0, 0),
      prev_dx_bipolar = c(0, 0, 0, 0, 0),
      prev_dx_generalized_anxiety = c(1,
                                      1, 0, 0, 0),
      prev_dx_major_depression_span = c(NA, NA, NA, NA,
                                        1),
      prev_dx_bipolar_span = c(NA, NA, NA, NA, NA),
      prev_dx_generalized_anxiety_span = c(NA,
                                           NA, NA, NA, 1)
    ),
    class = "data.frame",
    row.names = c(NA,-5L)
  ) 

CodePudding user response:

One option would be to

  1. Rename your variables to add a postfix engl to the english data columns
  2. Convert to long format such that we end up with a column containing variable names and two columns for Spanish and English data
  3. Get your unique values for each variable
  4. Convert back to wide format
library(dplyr)
library(tidyr)

rename_with(sample_data, ~ paste0(.x, "_engl"), .cols = !c(ends_with("_span"), id)) %>% 
  pivot_longer(-id, names_to = c("var", ".value"), names_pattern = "^(.*)_(.*)$") %>% 
  mutate(value = if_else(span %in% 1 | engl %in% 1, 1, 0)) %>% 
  select(-engl, -span) %>% 
  pivot_wider(names_from = var, values_from = value)
#> # A tibble: 5 × 4
#>      id prev_dx_major_depression prev_dx_bipolar prev_dx_generalized_anxiety
#>   <int>                    <dbl>           <dbl>                       <dbl>
#> 1     1                        0               0                           1
#> 2     2                        1               0                           1
#> 3     3                        1               0                           0
#> 4     4                        0               0                           0
#> 5     5                        1               0                           1
  • Related