将变量列表输入 table R

Feed list of variables into table R

这是我的例子:

set.seed(1)
col_1 <- sample(LETTERS[1:4], 1000, replace=TRUE)
col_2 <- sample(LETTERS[5:8],1000, replace = TRUE)
col_3 <- sample(LETTERS[9:12],1000, replace = TRUE)
sample_df <- data.frame(col_1,col_2,col_3) 

我可以通过以下行获得应急 table:

with(df,table(col_1, col_2))

如果我有很多变量,我想在 table 中使用矢量,像这样:

my_var <- c('col_1', 'col_2', 'col_3)
with(sample_df, table(my_var))

但它不起作用。有什么解决方法吗?

越简单越好!

table(sample_df[my_var])