# median

0th

Percentile

##### Median Value

Compute the sample median.

Keywords
robust, univar
##### Usage
median(x, na.rm = FALSE, …)
##### Arguments
x

an object for which a method has been defined, or a numeric vector containing the values whose median is to be computed.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

potentially further arguments for methods; not used in the default method.

##### Details

This is a generic function for which methods can be written. However, the default method makes use of is.na, sort and mean from package base all of which are generic, and so the default method will work for most classes (e.g., "Date") for which a median is a reasonable concept.

##### Value

The default method returns a length-one object of the same type as x, except when x is logical or integer of even length, when the result will be double.

If there are no values or if na.rm = FALSE and there are NA values the result is NA of the same type as x (or more generally the result of x[FALSE][NA]).

##### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

quantile for general quantiles.

##### Aliases
• median
• median.default
##### Examples
library(stats) # NOT RUN { median(1:4) # = 2.5 [even number] median(c(1:3, 100, 1000)) # = 3 [odd, robust] # } 
Documentation reproduced from package stats, version 3.6.0, License: Part of R 3.6.0

### Community examples

richie@datacamp.com at Jan 17, 2017 stats v3.3.1

The median of a set with an number of values is the middle one, when sorted from smallest to largest. {r} x <- c(1, 10, 5, 8, 9) median(x) sort(x) # same  If there are an even number of values, the median is half way between the two middle values. {r} x <- c(1, 10, 5, 8, 9, 6) median(x) sorted <- sort(x) (sorted + sorted) / 2 # same  If there are any missing values, the median is also missing. {r} median(c(1, 10, 5, 8, 9, NA))  You can exclude missing values by setting na.rm = TRUE. {r} median(c(1, 10, 5, 8, 9, NA), na.rm = TRUE)  The median is known as a robust estimator of location, since it ignores outliers. The following dataset has 10% taken from a wide distribution that will generate many outliers. The expected mean and median are both zero. Which one is closest? {r} x <- c(rnorm(900), rnorm(100, sd = 1000)) mean(x) median(x)