如何获得平均数,中位数,和其他在整个矩阵,数组或数据帧的统计数据? [英] 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
推荐答案
由于这来了一个公平一点,我打算把这个多一点COM prehensively,包括了等等。片中除了平均值
和中位数
。
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
.
-
对于一个矩阵或数组,因为其他人说,
的意思是
和中位数
将返回单个值。然而,VAR
将计算二维矩阵的列之间的协方差。有趣的是,对于一个多维阵列,无功
追溯到返回一个值。SD
上的2-D矩阵将工作,但pcated德$ P $,返回列的标准偏差。更妙的是,狂
返回上一个二维矩阵的和的多维数组的单个值。如果你的希望的一个返回值,最安全的方法是使用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
,的意思是
是德precated,但会再次采取行动上单独的列。 中位数
要求您强制为向量第一,或不公开
。和以前一样, VAR
将返回协方差和 SD
又是pcated德$ P $,但将返回标准偏差的列。 狂
要求您强制为向量或不公开
。一般来说一个 data.frame
如果你想要采取行动的所有值,你通常只会不公开
首先
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是defunctified:
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屋!