SJB
SJB

Reputation: 672

Generate summary / table statistics based on data type in R

I want to write an automated code in R for generating summary and table.

Statistics for variables based on their data types. for. e.g

If data type = Numeric, display summary();

If data type = factor, display table();

Please help me.

Thanks Balaji

Upvotes: 1

Views: 73

Answers (1)

Aleh
Aleh

Reputation: 826

This should do it

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)

Upvotes: 1

Related Questions