简体   繁体   English

如何在 psych::describe(mydata) output 中添加统计模式列?

[英]How to add a column for statistical mode to psych::describe(mydata) output?

Has anyone written code to add a column to the psych::describe() output that identifies the statistical mode for each variable?有没有人编写代码向psych::describe() output 添加一列来标识每个变量的统计模式? 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.例如,使用数据集mtcars ,我们可以使用psych package 轻松生成数据的描述性统计,但统计模式不包含在 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?鉴于psych::describe(mydata)的 output 不包含数据集中每个变量的统计模式信息,我们如何在psych::describe(mydata) output 中添加列以用于每个变量的统计模式数据集?

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.对于上下文,我添加和使用 function 来识别统计模式没有问题,但生成 output 需要我通过变量单独询问它。 I've tried various ways of adding a column for statistical mode to the psych::describe (mydata) output, but invariably there are weird errors.我尝试了各种方法将统计模式列添加到psych::describe (mydata) output,但总是会出现奇怪的错误。

To generate modes I have both written the function for it为了生成模式,我都为它编写了 function

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.并使用了 package DescTools ,但两者都需要我命名一个变量来生成它的模式。 I would like to do this for every column in the mtcars data frame.我想对mtcars数据框中的每一列执行此操作。

tldr; tldr; I would like to add a column after "se" in the psych::describe(mydata) output that describes the statistical mode for every variable.我想在psych::describe(mydata) output 中的“se”之后添加一列,描述每个变量的统计模式。 What do you recommend?你有什么建议吗?

You may cbind the additional function to describe output to get it as a new column.您可以将附加的cbind绑定到describe output 以将其作为新列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM