in R, I want to make function that when input is c() of number show mean, var, min ,max of it. But how can I align the output just like picture WITHOUT using summary() ?
is there any function which print like that?? EXCEPT summary()
If we want to get the summary of selected statistics, use lapply
to loop over the list
and get those output based on an if/else
condition
lapply(iris, function(x) if(is.numeric(x))
c(mean = mean(x), var = var(x), max = max(x), min = min(x)) else table(x))
-output
#$Sepal.Length
# mean var max min
#5.8433333 0.6856935 7.9000000 4.3000000
#$Sepal.Width
# mean var max min
#3.0573333 0.1899794 4.4000000 2.0000000
#$Petal.Length
# mean var max min
#3.758000 3.116278 6.900000 1.000000
#$Petal.Width
# mean var max min
#1.1993333 0.5810063 2.5000000 0.1000000
#$Species
#x
# setosa versicolor virginica
# 50 50 50
It can be wrapped into a function
summary_fun <- function(dat) {
lapply(dat, function(x)
if(is.numeric(x)) {
c(mean = mean(x), var = var(x), max = max(x), min = min(x))
} else if(is.character(x)|is.factor(x)) {
table(x)
} else NA
)
}
and then call
summary_fun(iris)