Dear All,
I have a survey data set and would like to disaggregate all content variables (79 vars on a likert scale) by several categorical variables (here I give the example of component_new).
Obviously, I can't copy and paste each variable individually.
So my question is how I can reproduce this code snipped for all Var_content columns.
tab <- round(prop.table(table(component_new, Var_content), margin = 1),2 )
ftable(tab)
Now, I would like to write a for loop over the columns but dont understand how I can use the indices.
My first column in the variable I'd like to disaggregate:
here the glimpse:
Observations: 6,166
Variables: 79
component_new <chr> "Military", "Unknown", "Civilian-IS", "Civilian-IS", "Civilian-IS",…
Mysupervisor_x 5, NA, 5, 4, 4, 5, NA, 1, 5, 5, 4, 1, 4, 4, 4, 4, 2, 4, 5, 5, 4, NA…
$ Mysupervisor_x 5, NA, 5, 4, 5, 5, NA, 1, 5, 5, 5, 2, 3, 4,
I unsuccessfully tried this:
By_ comp <- for (i in Var_cont) {
prop.comp <- round(prop.table(table(component_new, Var_cont[i]), margin = 1),2 ) %>%
ftable(prop.comp )
}
Also unsuccessful was to use an index in the form of:
for (i in Var_cont[,2:79]) {...}
or
Var_cont %>% for (i in [,2:79]) {...}
or
for(i in 2:ncol(Vars_cont)) {...}
the last option
for(i in 2:ncol(Vars_cont)) {
tab <- table(Vars_cont$component_new, Vars_cont[,i])
prop <- prop.table(tab, margin = 1)
print(prop)
}
Gives me the error message "Error in table(Vars_cont$component_new, Vars_cont[, i]) : all arguments must have the same length".
The other attempt dont even get an error message
Thank you very much and sorry for the terrible code!
Martin