Creating a table of means using R
When you cross-tab a categorical array by a categorical variable, you end up with different tabs (one for each column category). On each tab is a list of means.
If you want to create a quick summary table with only the means involved, you need to run a bit of R code to get this summary in Excel. This is a feature that should be available in a future version of Cruncg, so you don't need to use R code. Below, though, we try to make it as simple as possible for you to specify the categorical array and the column variable - and then the code should do the rest.
How to run the code.
- First you should login and load your dataset with R.
The following R code allows you to create a table of means. Things to need to change:
- my_array_alias - is the alias of the categorical array. This can be found under the Variable Properties.
- my_column_alias - is the alias of the column break you want. This can be found under Variable Properties.
- means_tab.csv - is the name of the output csv that will be produced in your working directory (use getwd() to see where this is)
array <- "my_array_alias"
columns <- "my_column_alias"
means_tab <- lapply(aliases(subvariables(ds[[array]])), function(vvv) {
my_var <- paste0("ds[['",array,"']][['", vvv, "']]")
crtabs(formula(paste0("mean(as.numeric(", my_var, ")) ~ ", columns)), ds) %>%
as.array() %>%
t() %>%
as.data.frame() %>%
mutate(var = vvv) %>%
select(var, everything())
}) %>% bind_rows()
write.csv(means_tab, "means_tab.csv")