【问题标题】:Generate summary / table statistics based on data type in R根据 R 中的数据类型生成汇总/表格统计信息
【发布时间】:2018-03-08 07:23:39
【问题描述】:

我想在 R 中编写一个自动代码来生成摘要和表格。

基于数据类型的变量统计。为了。例如

If data type = Numeric, display summary();

If data type = factor, display table();

请帮帮我。

谢谢 巴拉吉

【问题讨论】:

  • 你需要if (is.numeric(x)) { ... } else if (is.factor(x)) { ... } else { ... }。还有其他的吗?

标签: r


【解决方案1】:

应该这样做

data <- data.frame(a=rnorm(4), b=c("a", "a", "b", "b"), c=c(TRUE, FALSE, TRUE, FALSE))

Display <- function(x) {
    switch(
    EXPR = class(x),
    factor = {
       print(table(x))
    },
    numeric = {
       print(summary(x))
    },
    print("not supported type")
    )
 }

 Display(data$a)
 Display(data$b)
 Display(data$c)

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2018-08-11
    • 2014-01-15
    • 1970-01-01
    • 2023-02-21
    • 2018-06-26
    • 2019-08-18
    • 2021-08-09
    • 2014-04-09
    相关资源
    最近更新 更多