How to automatically generate Subtotals (aka NETs) on multiple questions with R
Overview
The Crunch web app allows you to add "Subtotals" (aka NETs) for only one variable at a time from variable Properties. Using Subtotals instead will save you time when there is a list of variables that needs exactly the same nets.
Using Subtotals
You can use the subtotals() function in R. The following example shows 8 subvariables at Q1 that need similar nets:
for (j in grep("Q1[1-8]$", aliases(variables(ds)), value=T)) { subtotals(ds[[j]]) <- list( Subtotal(name="Top 2 Box", categories=c(1,2), position="top"), Subtotal(name="Bottom 2 Box", categories=c(4,5), position="bottom") ) }
Similarly, you can add nets for multiple questions at a time, as follows:
for (j in c('Q1', 'Q2', 'Q3', 'Q4', 'Q5')) { subtotals(ds[[j]]) <- list( Subtotal(name="Top 3 Box", categories=c(5,6,7), position="top"), Subtotal(name="Bottom 3 Box", categories=c(1,2,3), position="top") ) }
In the following script, variables can be stored in one object if there are many that need exactly the same action:
net_vars <- c('Q1', 'Q2', 'Q3', 'Q4', 'Q5') for (j in net_vars) { subtotals(ds[[j]]) <- list( Subtotal(name="Top 3 Box", categories=c(5,6,7), position="top"), Subtotal(name="Bottom 3 Box", categories=c(1,2,3), position="top") ) }