Median

DESCRIPTION:

Returns a number that is the median of the data. Missing values can be removed before the computation.

USAGE:

median(x, na.rm = F, weights = NULL)

REQUIRED ARGUMENTS:

x
numeric object or bdNumeric. Missing values (NA) are allowed.

OPTIONAL ARGUMENTS:

na.rm
logical flag; indicates whether missing values should be removed before computation.
weights
non-negative numeric object of same size as x for computing weighted median (see below). Does not have to be normalized to sum to 1.

VALUE:

median of the data.

If argument weights is provided, x is sorted and weights are normalized to sum to 1. The median is then the value of x corresponding to the first cum-summed weight greater than or equal to .5. In case a cum-summed weight is equal to .5, the median is the average of the value of x corresponding to that weight and the value of x corresponding to the first cumsumed weight strictly greater than .5 (there may be intervening zero-valued weights, which are ignored).

DETAILS:

If x contains any NAs, the result will be NA unless na.rm=TRUE.

NOTE:

For long vectors or bdVectors, using quantile(x, .5) rather than median(x) is significantly faster, because quantile uses a partial sort, while median uses a sort. Note that quantile(x, .5, weights = weights) gives a different answer than median(x, weights = weights). Also, quantile uses single precision.

SEE ALSO:

, , , , .

EXAMPLES:

algebra <- testscores[,3] # create sample object of algebra testscores 
median(algebra) # median of scores 
apply(testscores, 2, median) # vector of the medians of the columns  
                             # in testscores data set