How to create a net on a multiple response variable using R
Sometimes you need to create a net on multiple response variables.
A net in this scenario, is defined as "at least one mention of". In Crunch, you can do this by creating a new subvariable which is added to the overall multiple response variable.
The code below derives a new copy of the multiple response variable to include the net. It hides the original MR variable from view.
The following 3 lines of code allows you to specify the things you want to change:
mr_var <- "var_alias"
cats_to_net <- c("sub_var1", "sub_var2")
net_name <- "NET - name to insert"
In the above:
- var_alias - is the alias of the variable in which you want to create the net
- cats_to_net - are the categories that you want to net, which you can specify by name here
- net_name - is the name of the net, to appear on the variable, tables, graphs
Once you've done the above, you can run the code below:
new_var <- paste0(mr_var, "_net", net_name)
ds[[new_var]] <- combineResponses(ds[[mr_var]], name = net_name, combinations = list(list(name = net_name, responses = cats_to_net)))
y <- urls(subvariables(ds[[mr_var]]))
z <- urls(subvariables(ds[[new_var]])[net_name])
new_alias <- paste0(mr_var, "_net")
ds[[new_alias]]<- deriveArray(c(z,y), name = paste(name(ds[[mr_var]]), "1"), description = description(ds[[mr_var]]))
hide(ds[[new_var]])
hide(ds[[mr_var]])