Skip to contents

Qualtrics produces datasets where a matrix set of checkbox questions have value labels (factor labels) representing the actual question. When presenting these questions in a figure or table, however, it is often useful to recode the value labels to something common, e.g. "Selected" and "Not selected".

Usage

recode_checkbox_sets(
  data,
  var_patterns = NULL,
  input_check_value = 1L,
  input_uncheck_value = NA_integer_,
  output_check_value = 2L,
  output_uncheck_value = 1L,
  output_all_unchecked_value = NA_integer_,
  output_check_label = "Selected",
  output_uncheck_label = "Not selected",
  output_all_unchecked_label = NULL,
  convert_to_factor = TRUE,
  keep_tmp_var = FALSE
)

Arguments

data

Data frame or tibble.

var_patterns

Character vector of regex search patterns that uniquely identify variables belonging to sets of batteries. Same as used in tidyselect's matches().

input_check_value, input_uncheck_value

Factor/integer values indicating checked and unchecked responses.

output_check_value, output_uncheck_value

Recoded values for checked and unchecked responses.

output_all_unchecked_value

If respondent has not checked any checkboxes, what should they be recoded as? Set to NULL to not recode.

output_check_label, output_uncheck_label, output_all_unchecked_label

Factor labels.

convert_to_factor

Flag. Whether to convert variables to factor (using output_*_label args) or leave as is.

keep_tmp_var

Whether to keep temporary variables for output_all_unchecked_value (mostly for debugging). Defaults to FALSE.

Value

Data frame/tibble.

Details

Furthermore, this function recodes all of the variables within a set to missing if all of the variables are missing - under the assumption that the respondent has then omitted them all. In other cases, e.g. one checkbox is checked, then the other variables can be assumed to be consciously "Not selected".

Examples

n <- 100
set.seed(1241)
tmp <-
data.frame(a_1 = factor(sample(c("Car", NA), size = n, replace = TRUE)),
           a_2 = factor(sample(c("Train", NA), size = n, replace = TRUE)),
           a_3 = factor(sample(c("Bus", NA), size = n, replace = TRUE)),
           a_4 = factor(sample(c("Flight", NA), size = n, replace = TRUE)),
           b_1 = factor(sample(c("Apple", NA), size = n, replace = TRUE)),
           b_2 = factor(sample(c("Orange", NA), size = n, replace = TRUE)),
           b_3 = factor(sample(c("Banana", NA), size = n, replace = TRUE)),
           b_4 = factor(sample(c("Grapes", NA), size = n, replace = TRUE)))
tmp2 <- recode_checkbox_sets(tmp, var_patterns = c("^a_", "^b_[[:digit:]]$"))
cbind(tmp, tmp2)
#>      a_1   a_2  a_3    a_4   b_1    b_2    b_3    b_4          a_1          a_2
#> 1   <NA> Train  Bus Flight Apple   <NA> Banana Grapes Not selected     Selected
#> 2   <NA> Train  Bus Flight Apple Orange Banana   <NA> Not selected     Selected
#> 3   <NA>  <NA>  Bus   <NA>  <NA>   <NA> Banana Grapes Not selected Not selected
#> 4    Car Train  Bus   <NA> Apple   <NA>   <NA> Grapes     Selected     Selected
#> 5   <NA>  <NA>  Bus Flight  <NA> Orange   <NA> Grapes Not selected Not selected
#> 6   <NA> Train  Bus   <NA> Apple   <NA> Banana Grapes Not selected     Selected
#> 7   <NA> Train <NA>   <NA> Apple   <NA>   <NA>   <NA> Not selected     Selected
#> 8   <NA> Train <NA> Flight Apple Orange Banana Grapes Not selected     Selected
#> 9    Car Train <NA> Flight Apple   <NA>   <NA>   <NA>     Selected     Selected
#> 10   Car Train  Bus   <NA>  <NA> Orange   <NA> Grapes     Selected     Selected
#> 11  <NA> Train  Bus   <NA>  <NA>   <NA>   <NA> Grapes Not selected     Selected
#> 12   Car  <NA> <NA>   <NA> Apple   <NA>   <NA>   <NA>     Selected Not selected
#> 13   Car Train <NA> Flight Apple Orange Banana Grapes     Selected     Selected
#> 14   Car  <NA>  Bus Flight  <NA> Orange   <NA>   <NA>     Selected Not selected
#> 15  <NA> Train <NA> Flight  <NA> Orange Banana   <NA> Not selected     Selected
#> 16  <NA> Train  Bus Flight  <NA>   <NA>   <NA>   <NA> Not selected     Selected
#> 17  <NA> Train <NA> Flight  <NA> Orange   <NA> Grapes Not selected     Selected
#> 18   Car  <NA>  Bus Flight Apple Orange   <NA>   <NA>     Selected Not selected
#> 19  <NA>  <NA>  Bus Flight  <NA>   <NA> Banana   <NA> Not selected Not selected
#> 20  <NA> Train <NA>   <NA> Apple   <NA> Banana   <NA> Not selected     Selected
#> 21  <NA> Train  Bus   <NA> Apple Orange   <NA> Grapes Not selected     Selected
#> 22   Car Train <NA> Flight  <NA> Orange   <NA>   <NA>     Selected     Selected
#> 23   Car Train  Bus Flight Apple   <NA>   <NA> Grapes     Selected     Selected
#> 24   Car Train <NA> Flight Apple   <NA>   <NA> Grapes     Selected     Selected
#> 25  <NA> Train <NA> Flight  <NA>   <NA> Banana   <NA> Not selected     Selected
#> 26   Car Train  Bus   <NA>  <NA> Orange   <NA> Grapes     Selected     Selected
#> 27  <NA>  <NA>  Bus Flight Apple   <NA>   <NA> Grapes Not selected Not selected
#> 28   Car Train <NA>   <NA>  <NA> Orange   <NA>   <NA>     Selected     Selected
#> 29  <NA> Train <NA>   <NA> Apple   <NA>   <NA>   <NA> Not selected     Selected
#> 30   Car Train <NA>   <NA>  <NA>   <NA> Banana Grapes     Selected     Selected
#> 31  <NA>  <NA>  Bus   <NA> Apple Orange   <NA> Grapes Not selected Not selected
#> 32  <NA>  <NA>  Bus Flight Apple Orange Banana Grapes Not selected Not selected
#> 33   Car  <NA> <NA>   <NA> Apple Orange   <NA>   <NA>     Selected Not selected
#> 34  <NA> Train <NA>   <NA>  <NA> Orange Banana Grapes Not selected     Selected
#> 35  <NA> Train <NA> Flight  <NA> Orange Banana   <NA> Not selected     Selected
#> 36  <NA>  <NA>  Bus   <NA>  <NA>   <NA>   <NA>   <NA> Not selected Not selected
#> 37   Car  <NA> <NA>   <NA>  <NA>   <NA>   <NA> Grapes     Selected Not selected
#> 38  <NA> Train <NA>   <NA> Apple Orange Banana   <NA> Not selected     Selected
#> 39   Car  <NA>  Bus   <NA> Apple   <NA> Banana   <NA>     Selected Not selected
#> 40   Car  <NA>  Bus   <NA> Apple Orange   <NA> Grapes     Selected Not selected
#> 41  <NA> Train <NA> Flight  <NA> Orange Banana   <NA> Not selected     Selected
#> 42   Car Train  Bus Flight Apple Orange   <NA>   <NA>     Selected     Selected
#> 43  <NA>  <NA> <NA>   <NA> Apple   <NA>   <NA>   <NA>         <NA>         <NA>
#> 44  <NA> Train  Bus   <NA> Apple Orange Banana   <NA> Not selected     Selected
#> 45   Car Train  Bus Flight Apple   <NA> Banana   <NA>     Selected     Selected
#> 46   Car Train <NA>   <NA>  <NA> Orange   <NA> Grapes     Selected     Selected
#> 47   Car Train <NA>   <NA>  <NA> Orange Banana Grapes     Selected     Selected
#> 48   Car  <NA>  Bus Flight Apple Orange   <NA>   <NA>     Selected Not selected
#> 49  <NA> Train  Bus   <NA> Apple Orange   <NA> Grapes Not selected     Selected
#> 50  <NA>  <NA> <NA> Flight Apple   <NA>   <NA>   <NA> Not selected Not selected
#> 51  <NA>  <NA>  Bus   <NA> Apple   <NA> Banana   <NA> Not selected Not selected
#> 52   Car  <NA> <NA>   <NA> Apple   <NA>   <NA>   <NA>     Selected Not selected
#> 53  <NA>  <NA>  Bus Flight  <NA>   <NA>   <NA> Grapes Not selected Not selected
#> 54   Car  <NA> <NA> Flight Apple   <NA> Banana   <NA>     Selected Not selected
#> 55  <NA>  <NA>  Bus   <NA>  <NA>   <NA>   <NA> Grapes Not selected Not selected
#> 56  <NA> Train <NA>   <NA> Apple   <NA> Banana   <NA> Not selected     Selected
#> 57  <NA>  <NA> <NA> Flight Apple Orange   <NA>   <NA> Not selected Not selected
#> 58  <NA> Train <NA> Flight  <NA>   <NA> Banana   <NA> Not selected     Selected
#> 59  <NA> Train  Bus   <NA>  <NA>   <NA>   <NA>   <NA> Not selected     Selected
#> 60   Car  <NA> <NA> Flight  <NA> Orange Banana   <NA>     Selected Not selected
#> 61  <NA>  <NA> <NA> Flight  <NA> Orange   <NA>   <NA> Not selected Not selected
#> 62   Car  <NA>  Bus   <NA>  <NA>   <NA> Banana Grapes     Selected Not selected
#> 63  <NA> Train  Bus   <NA> Apple   <NA>   <NA> Grapes Not selected     Selected
#> 64  <NA>  <NA>  Bus   <NA> Apple Orange   <NA>   <NA> Not selected Not selected
#> 65  <NA> Train  Bus   <NA>  <NA> Orange Banana   <NA> Not selected     Selected
#> 66  <NA>  <NA> <NA> Flight Apple   <NA>   <NA> Grapes Not selected Not selected
#> 67   Car Train  Bus Flight  <NA> Orange   <NA>   <NA>     Selected     Selected
#> 68   Car  <NA> <NA> Flight  <NA> Orange   <NA>   <NA>     Selected Not selected
#> 69   Car Train <NA>   <NA> Apple Orange   <NA>   <NA>     Selected     Selected
#> 70  <NA> Train  Bus   <NA> Apple Orange   <NA>   <NA> Not selected     Selected
#> 71   Car Train <NA>   <NA>  <NA> Orange Banana   <NA>     Selected     Selected
#> 72   Car Train <NA>   <NA> Apple Orange   <NA> Grapes     Selected     Selected
#> 73   Car  <NA>  Bus Flight Apple Orange   <NA> Grapes     Selected Not selected
#> 74  <NA> Train  Bus   <NA> Apple Orange   <NA> Grapes Not selected     Selected
#> 75  <NA>  <NA>  Bus   <NA>  <NA>   <NA> Banana   <NA> Not selected Not selected
#> 76   Car  <NA>  Bus Flight  <NA>   <NA> Banana Grapes     Selected Not selected
#> 77  <NA> Train <NA> Flight Apple   <NA> Banana Grapes Not selected     Selected
#> 78  <NA>  <NA> <NA> Flight  <NA>   <NA> Banana   <NA> Not selected Not selected
#> 79   Car  <NA>  Bus Flight Apple   <NA> Banana   <NA>     Selected Not selected
#> 80   Car  <NA> <NA>   <NA>  <NA> Orange Banana Grapes     Selected Not selected
#> 81  <NA> Train <NA>   <NA> Apple   <NA> Banana   <NA> Not selected     Selected
#> 82   Car  <NA>  Bus   <NA>  <NA> Orange   <NA> Grapes     Selected Not selected
#> 83  <NA> Train <NA>   <NA> Apple Orange Banana Grapes Not selected     Selected
#> 84   Car  <NA> <NA> Flight  <NA> Orange   <NA> Grapes     Selected Not selected
#> 85  <NA> Train  Bus   <NA> Apple Orange   <NA>   <NA> Not selected     Selected
#> 86   Car Train <NA> Flight  <NA>   <NA> Banana   <NA>     Selected     Selected
#> 87  <NA>  <NA> <NA>   <NA> Apple Orange   <NA>   <NA>         <NA>         <NA>
#> 88   Car Train  Bus   <NA> Apple   <NA> Banana Grapes     Selected     Selected
#> 89  <NA> Train  Bus Flight Apple   <NA> Banana   <NA> Not selected     Selected
#> 90  <NA>  <NA>  Bus Flight Apple Orange Banana Grapes Not selected Not selected
#> 91   Car  <NA> <NA>   <NA>  <NA> Orange   <NA>   <NA>     Selected Not selected
#> 92   Car Train  Bus   <NA> Apple   <NA> Banana Grapes     Selected     Selected
#> 93   Car Train  Bus Flight  <NA>   <NA> Banana Grapes     Selected     Selected
#> 94   Car Train  Bus Flight  <NA> Orange Banana   <NA>     Selected     Selected
#> 95  <NA>  <NA>  Bus Flight  <NA>   <NA>   <NA>   <NA> Not selected Not selected
#> 96  <NA>  <NA>  Bus Flight Apple Orange   <NA>   <NA> Not selected Not selected
#> 97  <NA>  <NA> <NA> Flight Apple Orange   <NA>   <NA> Not selected Not selected
#> 98   Car Train  Bus Flight Apple   <NA> Banana   <NA>     Selected     Selected
#> 99   Car Train  Bus   <NA> Apple Orange Banana   <NA>     Selected     Selected
#> 100 <NA> Train  Bus Flight Apple Orange Banana Grapes Not selected     Selected
#>              a_3          a_4          b_1          b_2          b_3
#> 1       Selected     Selected     Selected Not selected     Selected
#> 2       Selected     Selected     Selected     Selected     Selected
#> 3       Selected Not selected Not selected Not selected     Selected
#> 4       Selected Not selected     Selected Not selected Not selected
#> 5       Selected     Selected Not selected     Selected Not selected
#> 6       Selected Not selected     Selected Not selected     Selected
#> 7   Not selected Not selected     Selected Not selected Not selected
#> 8   Not selected     Selected     Selected     Selected     Selected
#> 9   Not selected     Selected     Selected Not selected Not selected
#> 10      Selected Not selected Not selected     Selected Not selected
#> 11      Selected Not selected Not selected Not selected Not selected
#> 12  Not selected Not selected     Selected Not selected Not selected
#> 13  Not selected     Selected     Selected     Selected     Selected
#> 14      Selected     Selected Not selected     Selected Not selected
#> 15  Not selected     Selected Not selected     Selected     Selected
#> 16      Selected     Selected         <NA>         <NA>         <NA>
#> 17  Not selected     Selected Not selected     Selected Not selected
#> 18      Selected     Selected     Selected     Selected Not selected
#> 19      Selected     Selected Not selected Not selected     Selected
#> 20  Not selected Not selected     Selected Not selected     Selected
#> 21      Selected Not selected     Selected     Selected Not selected
#> 22  Not selected     Selected Not selected     Selected Not selected
#> 23      Selected     Selected     Selected Not selected Not selected
#> 24  Not selected     Selected     Selected Not selected Not selected
#> 25  Not selected     Selected Not selected Not selected     Selected
#> 26      Selected Not selected Not selected     Selected Not selected
#> 27      Selected     Selected     Selected Not selected Not selected
#> 28  Not selected Not selected Not selected     Selected Not selected
#> 29  Not selected Not selected     Selected Not selected Not selected
#> 30  Not selected Not selected Not selected Not selected     Selected
#> 31      Selected Not selected     Selected     Selected Not selected
#> 32      Selected     Selected     Selected     Selected     Selected
#> 33  Not selected Not selected     Selected     Selected Not selected
#> 34  Not selected Not selected Not selected     Selected     Selected
#> 35  Not selected     Selected Not selected     Selected     Selected
#> 36      Selected Not selected         <NA>         <NA>         <NA>
#> 37  Not selected Not selected Not selected Not selected Not selected
#> 38  Not selected Not selected     Selected     Selected     Selected
#> 39      Selected Not selected     Selected Not selected     Selected
#> 40      Selected Not selected     Selected     Selected Not selected
#> 41  Not selected     Selected Not selected     Selected     Selected
#> 42      Selected     Selected     Selected     Selected Not selected
#> 43          <NA>         <NA>     Selected Not selected Not selected
#> 44      Selected Not selected     Selected     Selected     Selected
#> 45      Selected     Selected     Selected Not selected     Selected
#> 46  Not selected Not selected Not selected     Selected Not selected
#> 47  Not selected Not selected Not selected     Selected     Selected
#> 48      Selected     Selected     Selected     Selected Not selected
#> 49      Selected Not selected     Selected     Selected Not selected
#> 50  Not selected     Selected     Selected Not selected Not selected
#> 51      Selected Not selected     Selected Not selected     Selected
#> 52  Not selected Not selected     Selected Not selected Not selected
#> 53      Selected     Selected Not selected Not selected Not selected
#> 54  Not selected     Selected     Selected Not selected     Selected
#> 55      Selected Not selected Not selected Not selected Not selected
#> 56  Not selected Not selected     Selected Not selected     Selected
#> 57  Not selected     Selected     Selected     Selected Not selected
#> 58  Not selected     Selected Not selected Not selected     Selected
#> 59      Selected Not selected         <NA>         <NA>         <NA>
#> 60  Not selected     Selected Not selected     Selected     Selected
#> 61  Not selected     Selected Not selected     Selected Not selected
#> 62      Selected Not selected Not selected Not selected     Selected
#> 63      Selected Not selected     Selected Not selected Not selected
#> 64      Selected Not selected     Selected     Selected Not selected
#> 65      Selected Not selected Not selected     Selected     Selected
#> 66  Not selected     Selected     Selected Not selected Not selected
#> 67      Selected     Selected Not selected     Selected Not selected
#> 68  Not selected     Selected Not selected     Selected Not selected
#> 69  Not selected Not selected     Selected     Selected Not selected
#> 70      Selected Not selected     Selected     Selected Not selected
#> 71  Not selected Not selected Not selected     Selected     Selected
#> 72  Not selected Not selected     Selected     Selected Not selected
#> 73      Selected     Selected     Selected     Selected Not selected
#> 74      Selected Not selected     Selected     Selected Not selected
#> 75      Selected Not selected Not selected Not selected     Selected
#> 76      Selected     Selected Not selected Not selected     Selected
#> 77  Not selected     Selected     Selected Not selected     Selected
#> 78  Not selected     Selected Not selected Not selected     Selected
#> 79      Selected     Selected     Selected Not selected     Selected
#> 80  Not selected Not selected Not selected     Selected     Selected
#> 81  Not selected Not selected     Selected Not selected     Selected
#> 82      Selected Not selected Not selected     Selected Not selected
#> 83  Not selected Not selected     Selected     Selected     Selected
#> 84  Not selected     Selected Not selected     Selected Not selected
#> 85      Selected Not selected     Selected     Selected Not selected
#> 86  Not selected     Selected Not selected Not selected     Selected
#> 87          <NA>         <NA>     Selected     Selected Not selected
#> 88      Selected Not selected     Selected Not selected     Selected
#> 89      Selected     Selected     Selected Not selected     Selected
#> 90      Selected     Selected     Selected     Selected     Selected
#> 91  Not selected Not selected Not selected     Selected Not selected
#> 92      Selected Not selected     Selected Not selected     Selected
#> 93      Selected     Selected Not selected Not selected     Selected
#> 94      Selected     Selected Not selected     Selected     Selected
#> 95      Selected     Selected         <NA>         <NA>         <NA>
#> 96      Selected     Selected     Selected     Selected Not selected
#> 97  Not selected     Selected     Selected     Selected Not selected
#> 98      Selected     Selected     Selected Not selected     Selected
#> 99      Selected Not selected     Selected     Selected     Selected
#> 100     Selected     Selected     Selected     Selected     Selected
#>              b_4
#> 1       Selected
#> 2   Not selected
#> 3       Selected
#> 4       Selected
#> 5       Selected
#> 6       Selected
#> 7   Not selected
#> 8       Selected
#> 9   Not selected
#> 10      Selected
#> 11      Selected
#> 12  Not selected
#> 13      Selected
#> 14  Not selected
#> 15  Not selected
#> 16          <NA>
#> 17      Selected
#> 18  Not selected
#> 19  Not selected
#> 20  Not selected
#> 21      Selected
#> 22  Not selected
#> 23      Selected
#> 24      Selected
#> 25  Not selected
#> 26      Selected
#> 27      Selected
#> 28  Not selected
#> 29  Not selected
#> 30      Selected
#> 31      Selected
#> 32      Selected
#> 33  Not selected
#> 34      Selected
#> 35  Not selected
#> 36          <NA>
#> 37      Selected
#> 38  Not selected
#> 39  Not selected
#> 40      Selected
#> 41  Not selected
#> 42  Not selected
#> 43  Not selected
#> 44  Not selected
#> 45  Not selected
#> 46      Selected
#> 47      Selected
#> 48  Not selected
#> 49      Selected
#> 50  Not selected
#> 51  Not selected
#> 52  Not selected
#> 53      Selected
#> 54  Not selected
#> 55      Selected
#> 56  Not selected
#> 57  Not selected
#> 58  Not selected
#> 59          <NA>
#> 60  Not selected
#> 61  Not selected
#> 62      Selected
#> 63      Selected
#> 64  Not selected
#> 65  Not selected
#> 66      Selected
#> 67  Not selected
#> 68  Not selected
#> 69  Not selected
#> 70  Not selected
#> 71  Not selected
#> 72      Selected
#> 73      Selected
#> 74      Selected
#> 75  Not selected
#> 76      Selected
#> 77      Selected
#> 78  Not selected
#> 79  Not selected
#> 80      Selected
#> 81  Not selected
#> 82      Selected
#> 83      Selected
#> 84      Selected
#> 85  Not selected
#> 86  Not selected
#> 87  Not selected
#> 88      Selected
#> 89  Not selected
#> 90      Selected
#> 91  Not selected
#> 92      Selected
#> 93      Selected
#> 94  Not selected
#> 95          <NA>
#> 96  Not selected
#> 97  Not selected
#> 98  Not selected
#> 99  Not selected
#> 100     Selected