# 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

**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) # 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) ```