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
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
meanandmedian.For a matrix, or array, as the others have stated,
meanandmedianwill return a single value. However,varwill compute the covariances between the columns of a two dimensional matrix. Interestingly, for a multi-dimensional array,vargoes back to returning a single value.sdon a 2-d matrix will work, but is deprecated, returning the standard deviation of the columns. Even better,madreturns 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?For a
data.frame,meanis deprecated, but will again act on the columns separately.medianrequires that you coerce to a vector first, orunlist. As before,varwill return the covariances, andsdis again deprecated but will return the standard deviation of the columns.madrequires that you coerce to a vector orunlist. In general for adata.frameif you want something to act on all values, you generally will justunlistit first.Edit: Late breaking news(): In R 3.0.0 mean.data.frame is defunctified: