R table 函数应用所有列

R table function apply all columns

我想将 R 中的 table 函数应用于我的所有列。

我想查看我的 TARGET 和 ALL OTHER COLUMNS 的分布情况。这是一个例子:

table(df$target, df$variable1)

我有 500 列。我怎样才能对所有 500 列执行此操作?

table(df$target, df$variable1),
table(df$target, df$variable2),
table(df$target, df$variable3) 

不一一写? (申请?)

一个选项是 lapplysetdiff 遍历 'target' 以外的列名(假设我们需要所有其他列),然后得到 table 'target' 的列循环返回 list 的频率表

outlst <- lapply(setdiff(names(df), 'target'), 
      function(nm) table(df$target, df[[nm]]))