I want to apply table function in R to all my columns.
I want to see the distribution of my TARGET and ALL OTHER COLUMNS. Here is an exmaple:
table(df$target, df$variable1)
I have 500 columns. How can I do that for all my 500 columns?
table(df$target, df$variable1),
table(df$target, df$variable2),
table(df$target, df$variable3)
without writing one by one? (with apply?)
An option is lapply
to loop over the column names other than the 'target' with setdiff
(assuming we need all other columns), then get the table
of the 'target' and the column looped returning a list
of frequency tables
outlst <- lapply(setdiff(names(df), 'target'),
function(nm) table(df$target, df[[nm]]))