【问题标题】:Feed list of variables into table R将变量列表输入表 R
【发布时间】:2017-05-25 08:04:33
【问题描述】:

这是我的例子:

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) 

我可以通过以下行得到列联表:

with(df,table(col_1, col_2))

如果我有很多变量,我想在表格中使用向量,如下所示:

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

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

【问题讨论】:

  • @joel.wilson 非常感谢它的工作!

标签: r vector contingency


【解决方案1】:

尽可能简单!

table(sample_df[my_var])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多