如何获得整个矩阵、数组或数据框的均值、中位数和其他统计数据? [英] How to get mean, median, and other statistics over entire matrix, array or dataframe?
问题描述
我知道这是一个基本问题,但由于某些奇怪的原因我无法找到答案.
I know this is a basic question but for some strange reason I am unable to find an answer.
我应该如何在整个数组、矩阵或数据框上应用基本的统计函数(如均值、中位数等)来获得唯一的答案,而不是行或列上的向量
How should I apply basic statistical functions like mean, median, etc. over entire array, matrix or dataframe to get unique answers and not a vector over rows or columns
推荐答案
由于这个问题有点多,我将更全面地处理这个问题,包括等".除了 mean
和 median
.
Since this comes up a fair bit, I'm going to treat this a little more comprehensively, to include the 'etc.' piece in addition to mean
and median
.
对于矩阵或数组,正如其他人所说,
mean
和median
将返回单个值.但是,var
将计算二维矩阵的列之间的协方差.有趣的是,对于多维数组,var
返回返回单个值.sd
在二维矩阵上可以工作,但已弃用,返回列的标准偏差.更好的是,mad
返回二维矩阵和多维数组上的单个值.如果您想要返回单个值,最安全的方法是首先使用as.vector()
进行强制.玩得开心吗?
For a matrix, or array, as the others have stated,
mean
andmedian
will return a single value. However,var
will compute the covariances between the columns of a two dimensional matrix. Interestingly, for a multi-dimensional array,var
goes back to returning a single value.sd
on a 2-d matrix will work, but is deprecated, returning the standard deviation of the columns. Even better,mad
returns a single value on a 2-d matrix and a multi-dimensional array. If you want a single value returned, the safest route is to coerce usingas.vector()
first. Having fun yet?
对于 data.frame
,mean
已被弃用,但会再次单独作用于列.median
要求您先强制转换为向量,或者 unlist
.和以前一样,var
将返回协方差,sd
再次被弃用,但将返回列的标准偏差.mad
要求您强制转换为向量或 unlist
.一般来说,对于 data.frame
,如果您想要对所有值进行操作,您通常只需 unlist
首先.
For a data.frame
, mean
is deprecated, but will again act on the columns separately. median
requires that you coerce to a vector first, or unlist
. As before, var
will return the covariances, and sd
is again deprecated but will return the standard deviation of the columns. mad
requires that you coerce to a vector or unlist
. In general for a data.frame
if you want something to act on all values, you generally will just unlist
it first.
最新突发新闻():在 R 3.0.0 mean.data.frame 已失效:
Late breaking news(): In R 3.0.0 mean.data.frame is defunctified:
o mean() for data frames and sd() for data frames and matrices are
defunct.
这篇关于如何获得整个矩阵、数组或数据框的均值、中位数和其他统计数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!