Gathering Multiple-Response Crunch Variables
Usage
crunch_gather_vars(
crunch_ds,
var_alias,
var_name,
var_description = "",
var_pattern,
replace_regex = NULL,
reorder = FALSE,
dichotomize = FALSE,
main_level = "Selected"
)Arguments
- crunch_ds
Crunch Dataset
- var_alias
The alias you want the new MR variable to have.
- var_name
The name you want the new MR variable to have. This will be displayed in crunch.
- var_description
The description of the MR variable. Defaults to the description of one of the gathered subvariables.
- var_pattern
The regex pattern that all variables you'd like to gather have in common.
- replace_regex
The Regex code to extract desired strings from subvariable descriptions. Is used in
stringr::str_extract(). Consider RVerbalExpressions to build your regex expression. Whenreplace_regexis a character scalar, replace the names of the gather variables with something more intuitive from the subvariable description. By default the parameter is set toNULLwhich means that names will not be replaced.- reorder
Logical. Should variable be ordered on
main_level?.- dichotomize
Logical. Should variable be dichotomized on
main_level?. In the dashboard instead of a table with percentages for each of the levels you'll see a set of bars corresponding to themain_level. For more details, see crunch::dichotomize.- main_level
Character. The name of the field to order and dichotomize by.
Examples
if (FALSE) { # \dontrun{
library(crunch)
library(haven)
data("ds_survey_example", package = "g6tr.survey")
write_sav(ds_survey_example, path = "temp.sav")
newDataset(x = "temp.sav", name = "survey_example_test")
file.remove("temp.sav")
# Start
crunch_ds <- loadDataset("survey_example_test")
unlock(crunch_ds)
webApp(crunch_ds) ## Have a look at the dashboard on crunch
## With Regex + reorder + dichotomize
crunch_gather_vars(
crunch_ds,
var_alias = "s_major",
var_name = "Major",
var_description = "Something Major",
var_pattern = "^s_major",
replace_regex = "(?<=:).*(?=\\-)", #Extract the part between : and -
reorder = TRUE,
dichotomize = TRUE
)
## Without Regex
crunch_gather_vars(
crunch_ds,
var_alias = "career_learnr",
var_name = "Career Learn",
var_description = "Career Learn",
var_pattern = "^career_learnr",
replace_regex = NULL
)
webApp(crunch_ds) ## To see changes on Crunch
## Example of bad post-processed descriptions
## Duplicated records
crunch_gather_vars(
crunch_ds,
var_alias = "career_attract",
var_name = "Career Attraction",
var_description = "Career Attraction",
var_pattern = "^career_attractr",
replace_regex = srv_regex("after_phrase","in tech,")
# Extract everything after "in tech,"
)
## NA records
crunch_gather_vars(
crunch_ds,
var_alias = "career_attract",
var_name = "Career Attraction",
var_description = "Career Attraction",
var_pattern = "^career_attractr",
replace_regex = "s$"
)
# Delete Crunch dataset after testing function
# deleteDataset(crunch_ds)
} # }