# mean

##### Arithmetic Mean

Generic function for the (trimmed) arithmetic mean.

- Keywords
- univar

##### Usage

`mean(x, …)`# S3 method for default
mean(x, trim = 0, na.rm = FALSE, …)

##### Arguments

- x
An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Complex vectors are allowed for

`trim = 0`

, only.- trim
the fraction (0 to 0.5) of observations to be trimmed from each end of

`x`

before the mean is computed. Values of trim outside that range are taken as the nearest endpoint.- na.rm
a logical value indicating whether

`NA`

values should be stripped before the computation proceeds.- …
further arguments passed to or from other methods.

##### Value

If `trim`

is zero (the default), the arithmetic mean of the
values in `x`

is computed, as a numeric or complex vector of
length one. If `x`

is not logical (coerced to numeric), numeric
(including integer) or complex, `NA_real_`

is returned, with a warning.

If `trim`

is non-zero, a symmetrically trimmed mean is computed
with a fraction of `trim`

observations deleted from each end
before the mean is computed.

##### References

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

##### See Also

`weighted.mean`

, `mean.POSIXct`

,
`colMeans`

for row and column means.

##### Examples

`library(base)`

```
# NOT RUN {
x <- c(0:10, 50)
xm <- mean(x)
c(xm, mean(x, trim = 0.10))
# }
```

*Documentation reproduced from package base, version 3.5.1, License: Part of R 3.5.1*

### Community examples

**sld1@williams.edu**at Jan 12, 2020 base v3.6.2

**sld1@williams.edu**at Jan 12, 2020 base v3.6.2

**sld1@williams.edu**at Jan 12, 2020 base v3.6.2

**sld1@williams.edu**at Jan 12, 2020 base v3.6.2

**sld1@williams.edu**at Jan 8, 2020 base v3.6.2

**sld1@williams.edu**at Jan 8, 2020 base v3.6.2

**sld1@williams.edu**at Jan 8, 2020 base v3.6.2

**muskaanjain246@gmail.com**at Sep 22, 2019 base v3.6.1

num<- c(5,2,3,6.4,na) xy<- mean(num) print(xy)

**muskaanjain246@gmail.com**at Sep 22, 2019 base v3.6.1

**naman.upadhyay@duke.edu**at Jul 31, 2019 base v3.6.1

**habk18@yahoo.com**at May 31, 2019 base v3.6.0

**habk18@yahoo.com**at May 31, 2019 base v3.6.0

**austineokenu@gmail.com**at May 26, 2019 base v3.6.0

**kaungnyeinaye80@gmail.com**at Apr 29, 2019 base v3.6.0

**mridul.khanna.2017@sse.ac.in**at Apr 29, 2019 base v3.6.0

**brezeseid@gmail.com**at Apr 21, 2019 base v3.5.3

ph<- c(1,1,2,3,5,8,21) mean(ph)

**acbellaver@hotmail.com**at Apr 8, 2019 base v3.5.3

**ahm168**at Mar 20, 2019 base v3.5.3

**DATA ** `total_data <- c(4, 5, 2, 7,10, 8, 8, 5, 6, 1)` n : 10 **NORMAL MEAN** `mean_total_data <- mean(total_data)` **TRIMMED MEAN** `trimmed_mean_total_data <- mean(total_data, trim = 0.1) ` With "trim = 0.1", so from data population, n=10, will be sorted and trimmed ( 0.1 * 10 = 1) (10% from n population), for each initial and end of data, into n=8 from (4, 5, 2, 7,10, 8, 8, 5, 6, 1) sorted into (1, 2, 4, 5, 5, 6, 7, 8, 8, 10) then trimmed 0.1 into (2, 2, 4, 5, 5, 6, 7, 8, 8)

**yemikhafid@yahoo.com**at Mar 12, 2019 base v3.5.3

Age_Children mean(x, …) mean(x, trim = 0, na.rm = FALSE, …) c(12, 10, 9, 9, 7, 8, 5) value <- mean(Age_Children) print(value)

**yemikhafid@yahoo.com**at Mar 12, 2019 base v3.5.3

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**germancb79@gmail.com**at Feb 14, 2019 base v3.5.2

**salma.abdeljalil@gmail.com**at Jan 10, 2019 base v3.5.2

mean(c(2,6,9,9,9,8)

**salma.abdeljalil@gmail.com**at Jan 10, 2019 base v3.5.2

**salma.abdeljalil@gmail.com**at Jan 10, 2019 base v3.5.2

**Eve.lyttle@gmail.com**at Jan 2, 2019 base v3.5.2

**Eve.lyttle@gmail.com**at Jan 2, 2019 base v3.5.2

**Eve.lyttle@gmail.com**at Jan 2, 2019 base v3.5.2

**Eve.lyttle@gmail.com**at Jan 2, 2019 base v3.5.2

**Eve.lyttle@gmail.com**at Jan 2, 2019 base v3.5.2

**sinyoffers@gmail.com**at Sep 14, 2018 base v3.5.1

values <- c(3, 5, 7, 4, 9, 2, 8, 6, 3, 6, 7) mean(values, trim = .2) mean(values, trim = .3) mean(values, trim = .5)

**sinyoffers@gmail.com**at Sep 14, 2018 base v3.5.1

**sinyoffers@gmail.com**at Sep 14, 2018 base v3.5.1

**sinyoffers@gmail.com**at Sep 14, 2018 base v3.5.1

**lucianaioja11@gmail.com**at Sep 13, 2018 base v3.5.1

**lucianaioja11@gmail.com**at Sep 13, 2018 base v3.5.1

**lucianaioja11@gmail.com**at Sep 13, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

function (x, ...) NULL

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**neralla.ravi@gmail.com**at Sep 8, 2018 base v3.5.1

**ch.asdeshwal@gmail.com**at Sep 1, 2018 base v3.5.1

x <- c(11, 12, 16, NA) mean(x, TRUE)

**ch.asdeshwal@gmail.com**at Sep 1, 2018 base v3.5.1

**conquistadordeabuga@gmail.com**at Aug 25, 2018 base v3.5.1

mean(c(1:9,NA), trim=0.1, na.rm = TRUE)

**u15023045@tuks.co.za**at Aug 22, 2018 base v3.5.1

**wzhang001@hust.edu.cn**at Aug 17, 2018 base v3.5.1

**wzhang001@hust.edu.cn**at Aug 17, 2018 base v3.5.1

**wzhang001@hust.edu.cn**at Aug 16, 2018 base v3.5.1

**huadan.song@gmail.com**at Aug 9, 2018 base v3.5.1

**edgk_307@yahoo.co.in**at Jul 21, 2018 base v3.5.1

a<-c(1,2,3:10) b<-mean(a) print(b)

**edgk_307@yahoo.co.in**at Jul 21, 2018 base v3.5.1

**rashidamuckadam@gmail.com**at Jul 21, 2018 base v3.5.1

data <- ( 1,2,3,4,5,8) y<-mean(data) print(y)

**rashidamuckadam@gmail.com**at Jul 21, 2018 base v3.5.1

**norbert.nsereko@gmail.com**at Jul 18, 2018 base v3.5.1

**norbert.nsereko@gmail.com**at Jul 18, 2018 base v3.5.1

**chauhanlucky080@gmail.com**at Jul 10, 2018 base v3.5.1

**joakinmtz@gmail.com**at Jun 25, 2018 base v3.5.0

#Nuevo ejemplo > datos<-(2,4,8,9,9,10) > media<-mean(datos) > media

**joakinmtz@gmail.com**at Jun 25, 2018 base v3.5.0

**iglesiaschina@gmail.com**at Jun 5, 2018 base v3.5.0

data <- c(1, 2, 4, 5, 6) a <- sd (data) print (a)

**iglesiaschina@gmail.com**at Jun 5, 2018 base v3.5.0

**sarahnjeru90@gmail.com**at Jun 4, 2018 base v3.5.0

**Sreya**at May 14, 2018 base v3.5.0

**pfisterer.thomas@freenet.de**at May 2, 2018 base v3.5.0

data <- c(1:10,NA, 12:21) print(mean(data, na.rm=TRUE))

**pfisterer.thomas@freenet.de**at May 2, 2018 base v3.5.0

**derya.sahin@sesdata.com**at Apr 25, 2018 base v3.5.0

**derya.sahin@sesdata.com**at Apr 25, 2018 base v3.5.0

**dileepmungara@gmail.com**at Mar 29, 2018 base v3.4.3

**zhaozhouxia@gmail.com**at Mar 23, 2018 base v3.4.3

data <- c(1, 2, 3, 4, 5, 6) b <- mean(data) print(b)

**zhaozhouxia@gmail.com**at Mar 23, 2018 base v3.4.3

**zhaozhouxia@gmail.com**at Mar 23, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**geetanjali.devdikar@gmail.com**at Mar 16, 2018 base v3.4.3

**vanderleymatias@gmail.com**at Mar 10, 2018 base v3.4.3

**vanderleymatias@gmail.com**at Mar 10, 2018 base v3.4.3

**vanderleymatias@gmail.com**at Mar 10, 2018 base v3.4.3

**sarah.resch@barclays.com**at Mar 4, 2018 base v3.4.3

**sarah.resch@barclays.com**at Mar 4, 2018 base v3.4.3

**AnamariaSuciu**at Mar 4, 2018 base v3.4.3

#new example data <- c(4.5,6.7,3.6,4.4) mean <- mean(data) mean

**AnamariaSuciu**at Mar 4, 2018 base v3.4.3

**50141201@mylife.unisa.ac.za**at Feb 26, 2018 base v3.4.3

**doungkamol130@gmail.com**at Feb 13, 2018 base v3.4.3

**suryajit.maddy@gmail.com**at Feb 9, 2018 base v3.4.3

a<-c(1,24,5,6,7,8,8,9,7,5,4,3,3,32,3,55,6,NA) mean(a,na.rm=TRUE)

**ajs17g@my.fsu.edu**at Feb 1, 2018 base v3.4.3

**st059410@student.spbu.ru**at Jan 30, 2018 base v3.4.3

**tylercook@ufl.edu**at Jan 29, 2018 base v3.4.3

x<-c(1,2,3,4) y<-mean(x) print(y)

**tylercook@ufl.edu**at Jan 29, 2018 base v3.4.3

**kalest**at Jan 18, 2018 base v3.4.3

> Data <- c(2,9,2,-2,4) > a <- mean(Data) > a

**kalest**at Jan 18, 2018 base v3.4.3

**makhethapalesa9@gmail.com**at Dec 11, 2017 base v3.4.3

scores <- c(56,77,68,94,88,63) AvgScores <- mean(scores) print (AvgScore)

**makhethapalesa9@gmail.com**at Dec 11, 2017 base v3.4.3

Scores <- c(88, 76,45,65, 56,89,94,34) AvgScore <- mean(Scores) print (AvgScore)

**makhethapalesa9@gmail.com**at Dec 11, 2017 base v3.4.3

**danielzmbr84@gmail.com**at Dec 8, 2017 base v3.4.3

**danielzmbr84@gmail.com**at Dec 8, 2017 base v3.4.3

**danielzmbr84@gmail.com**at Dec 8, 2017 base v3.4.3

**malochka_a**at Dec 4, 2017 base v3.4.3

**atauskey@gmail.com**at Dec 2, 2017 base v3.4.3

```r data <- c(1,2,3,4,5,6,6,7,x,9) mean <- mean(data, na.rm = TRUE) print(mean) ```

**inesteix@aol.com**at Nov 7, 2017 base v3.4.1

**inesteix@aol.com**at Nov 7, 2017 base v3.4.1

**devin.clementi@anu.edu.au**at Jul 20, 2017 base v3.4.1

**nlujambio@outlook.com**at May 11, 2017 base v3.4.0

require(base) + mean(c(6 , 7, 7, 7, 8, 8, 8, 9)) + mean(c(1, 2, NA), na.rm = TRUE) + print

**jorge.rdz3288@gmail.com**at May 8, 2017 base v3.4.0

#data data <- c(14, 16, 19, 20, 21, 23) a <- mean(data) print(a)

**callegari1978@gmail.com**at Feb 16, 2017 base v3.3.2

## Mean of a matrix ```r values <- c(1:9) mat <- matrix(as.vector(values), nrow=3, ncol=3) av<-mean(mat) print(av) ```

**richie@datacamp.com**at Jan 17, 2017 base v3.3.2

![taylor-swift-meanness](http://i.giphy.com/118BgDvP27VbSo.gif) The mean of random numbers should be close to (but not exactly) what you ask for: ```{r} x <- rnorm(1000, mean = 5) mean(x) ``` If there are any missing values, the mean is also missing. ```{r} mean(c(1, 2, NA)) ``` You can exclude missing values by setting `na.rm = TRUE`. ```{r} mean(c(1, 2, NA), na.rm = TRUE) ``` The `trim` argument removes the lowest and highest values. This makes your estimate of the population mean more robust when you have extreme outliers. The following dataset has 10% taken from a wide distribution that will generate many outliers. The mean you expect to get is (close to) 0. ```{r} x <- c(rnorm(900), rnorm(100, sd = 1000)) mean(x) mean(x, trim = 0.1) # trim is equivalent to filtering by quantile mean(x[x > quantile(x, 0.1) & x < quantile(x, 0.9)]) # same # median is another robust estimator of location median(x) ``` If `x` has length 0, the result is [`NaN`](https://www.rdocumentation.org/packages/base/topics/is.finite). ```{r} mean(numeric()) ``` Unlike `sum()`, you can't spread the values across multiple inputs. ```{r} mean(4, 6, 7, 3, 5) # Probably not what you wanted mean(c(4, 6, 7, 3, 5)) ```

**Anahí**at Nov 13, 2016 base v3.3.1

**Mean** ```r fit = (1,3,5,7) mean_fit = mean(fit) print(mean_fit) ```

**Anahí**at Nov 13, 2016 base v3.3.1

**Sd** ```r data <- c(2,4,6,3) var <- sd(var) print(data) ```

**filipsch**at Oct 19, 2016 base v3.3.1

Depending on the value of `na.rm`, you can get different results if you're dealing with missing values: ```r mean(c(1:10, NA, 12:20)) mean(c(1:10, NA, 12:20), na.rm = TRUE) ```

**jake@datacamp.com**at Oct 11, 2016 base v3.3.1

```r data <- c(12, 14, 15, 20, 23, 30) a <- mean(data) print(a) ```