Here’s a possible function resumo_df
.
- First check whether the argument
X
is a data.frame and gives error if it is not;
- Next, determine which numerical columns;
- And for each numerical column calls the internal function
f
where the accounts are made.
Then you just put it all together do.call/cbind
.
resumo_df <- function(X, na.rm = FALSE){
f <- function(x, na.rm = FALSE){
xbar <- mean(x, na.rm = na.rm)
s <- sd(x, na.rm = na.rm)
cv <- s/xbar
c(
Min = min(x, na.rm = na.rm),
Mediana = median(x, na.rm = na.rm),
Max = max(x, na.rm = na.rm),
Ampl = diff(range(x, na.rm = na.rm)),
Var = var(x, na.rm = na.rm),
DP = sd(x, na.rm = na.rm),
CV = cv,
N = length(unique(x))
)
}
stopifnot(is.data.frame(X))
i <- sapply(X, is.numeric)
Y <- lapply(X[i], f, na.rm = na.rm)
do.call(cbind.data.frame, Y)
}
resumo_df(1:10)
# Error in resumo_df(1:10) : inherits(X, "data.frame") is not TRUE
resumo_df(tibble::as_tibble(iris))
# Sepal.Length Sepal.Width Petal.Length Petal.Width
#Min 4.3000000 2.0000000 1.000000 0.1000000
#Mediana 5.8000000 3.0000000 4.350000 1.3000000
#Max 7.9000000 4.4000000 6.900000 2.5000000
#Media 5.8433333 3.0573333 3.758000 1.1993333
#Ampl 3.6000000 2.4000000 5.900000 2.4000000
#Var 0.6856935 0.1899794 3.116278 0.5810063
#DP 0.8280661 0.4358663 1.765298 0.7622377
#CV 7.0566023 7.0143836 2.128819 1.5734375
#N 35.0000000 23.0000000 43.000000 22.0000000
resumo_df(mtcars)
# mpg cyl disp hp drat wt qsec vs am gear carb
#Min 10.400000 4.000000 71.100000 52.00000 2.7600000 1.5130000 14.500000 0.0000000 0.0000000 3.0000000 1.000000
#Mediana 19.200000 6.000000 196.300000 123.00000 3.6950000 3.3250000 17.710000 0.0000000 0.0000000 4.0000000 2.000000
#Max 33.900000 8.000000 472.000000 335.00000 4.9300000 5.4240000 22.900000 1.0000000 1.0000000 5.0000000 8.000000
#Media 20.090625 6.187500 230.721875 146.68750 3.5965625 3.2172500 17.848750 0.4375000 0.4062500 3.6875000 2.812500
#Ampl 23.500000 4.000000 400.900000 283.00000 2.1700000 3.9110000 8.400000 1.0000000 1.0000000 2.0000000 7.000000
#Var 36.324103 3.189516 15360.799829 4700.86694 0.2858814 0.9573790 3.193166 0.2540323 0.2489919 0.5443548 2.608871
#DP 6.026948 1.785922 123.938694 68.56287 0.5346787 0.9784574 1.786943 0.5040161 0.4989909 0.7378041 1.615200
#CV 3.333466 3.464598 1.861581 2.13946 6.7265860 3.2880837 9.988426 0.8680278 0.8141431 4.9979394 1.741270
#N 25.000000 3.000000 27.000000 22.00000 22.0000000 29.0000000 30.000000 2.0000000 2.0000000 3.0000000 6.000000