Has anyone written code to add a column to the psych::describe()
output that identifies the statistical mode for each variable? Or, how would you go about achieving this?
For example, using the dataset mtcars
, we can easily generate descriptive statistics for the data using the psych
package, but the statistical mode is not included in the output.
> mydata = mtcars
> head(mydata)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> library(psych)
> describe(mydata)
vars n mean sd median trimmed mad min max range skew kurtosis se
mpg 1 32 20.09 6.03 19.20 19.70 5.41 10.40 33.90 23.50 0.61 -0.37 1.07
cyl 2 32 6.19 1.79 6.00 6.23 2.97 4.00 8.00 4.00 -0.17 -1.76 0.32
disp 3 32 230.72 123.94 196.30 222.52 140.48 71.10 472.00 400.90 0.38 -1.21 21.91
hp 4 32 146.69 68.56 123.00 141.19 77.10 52.00 335.00 283.00 0.73 -0.14 12.12
drat 5 32 3.60 0.53 3.70 3.58 0.70 2.76 4.93 2.17 0.27 -0.71 0.09
wt 6 32 3.22 0.98 3.33 3.15 0.77 1.51 5.42 3.91 0.42 -0.02 0.17
qsec 7 32 17.85 1.79 17.71 17.83 1.42 14.50 22.90 8.40 0.37 0.34 0.32
vs 8 32 0.44 0.50 0.00 0.42 0.00 0.00 1.00 1.00 0.24 -2.00 0.09
am 9 32 0.41 0.50 0.00 0.38 0.00 0.00 1.00 1.00 0.36 -1.92 0.09
gear 10 32 3.69 0.74 4.00 3.62 1.48 3.00 5.00 2.00 0.53 -1.07 0.13
carb 11 32 2.81 1.62 2.00 2.65 1.48 1.00 8.00 7.00 1.05 1.26 0.29
Given that the output of psych::describe(mydata)
does not include information for the statistical mode for each variable in the dataset, how can we add a column to the psych::describe(mydata)
output for statistical mode for each variable in the dataset?
For context, I have no problem adding and using a function for identifying statistical mode, but generating the output for it requires me to ask for it individually by variable. I've tried various ways of adding a column for statistical mode to the psych::describe (mydata)
output, but invariably there are weird errors.
To generate modes I have both written the function for it
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
and used the package DescTools
, but both require me to name a variable to generate its mode. I would like to do this for every column in the mtcars
data frame.
tldr; I would like to add a column after "se" in the psych::describe(mydata)
output that describes the statistical mode for every variable. What do you recommend?
You may cbind
the additional function to describe
output to get it as a new column.
library(psych)
result <- cbind(describe(mydata), Mode = sapply(mydata, getmode))
result
# vars n mean sd median trimmed mad min max range skew kurtosis se Mode
#mpg 1 32 20.09 6.03 19.2 19.70 5.41 10.4 33.9 23.5 0.61 -0.373 1.065 21.0
#cyl 2 32 6.19 1.79 6.0 6.23 2.97 4.0 8.0 4.0 -0.17 -1.762 0.316 8.0
#disp 3 32 230.72 123.94 196.3 222.52 140.48 71.1 472.0 400.9 0.38 -1.207 21.909 275.8
#hp 4 32 146.69 68.56 123.0 141.19 77.10 52.0 335.0 283.0 0.73 -0.136 12.120 110.0
#drat 5 32 3.60 0.53 3.7 3.58 0.70 2.8 4.9 2.2 0.27 -0.715 0.095 3.9
#wt 6 32 3.22 0.98 3.3 3.15 0.77 1.5 5.4 3.9 0.42 -0.023 0.173 3.4
#qsec 7 32 17.85 1.79 17.7 17.83 1.42 14.5 22.9 8.4 0.37 0.335 0.316 17.0
#vs 8 32 0.44 0.50 0.0 0.42 0.00 0.0 1.0 1.0 0.24 -2.002 0.089 0.0
#am 9 32 0.41 0.50 0.0 0.38 0.00 0.0 1.0 1.0 0.36 -1.925 0.088 0.0
#gear 10 32 3.69 0.74 4.0 3.62 1.48 3.0 5.0 2.0 0.53 -1.070 0.130 3.0
#carb 11 32 2.81 1.62 2.0 2.65 1.48 1.0 8.0 7.0 1.05 1.257 0.286 4.0