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))
# }
Community examples
data <- c(1,2,3,4,5,6,7) a <- mean(data) print(a)
num<- c(5,2,3,6.4,na) xy<- mean(num) print(xy)
ph<- c(1,1,2,3,5,8,21) mean(ph)
**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)
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)
mean(c(2,6,9,9,9,8)
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)
function (x, ...) NULL
x <- c(11, 12, 16, NA) mean(x, TRUE)
mean(c(1:9,NA), trim=0.1, na.rm = TRUE)
a<-c(1,2,3:10) b<-mean(a) print(b)
data <- ( 1,2,3,4,5,8) y<-mean(data) print(y)
#Nuevo ejemplo > datos<-(2,4,8,9,9,10) > media<-mean(datos) > media
data <- c(1, 2, 4, 5, 6) a <- sd (data) print (a)
data <- c(1:10,NA, 12:21) print(mean(data, na.rm=TRUE))
data <- c(1, 2, 3, 4, 5, 6) b <- mean(data) print(b)
#new example data <- c(4.5,6.7,3.6,4.4) mean <- mean(data) mean
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)
x<-c(1,2,3,4) y<-mean(x) print(y)
> Data <- c(2,9,2,-2,4) > a <- mean(Data) > a
scores <- c(56,77,68,94,88,63) AvgScores <- mean(scores) print (AvgScore)
Scores <- c(88, 76,45,65, 56,89,94,34) AvgScore <- mean(Scores) print (AvgScore)
```r data <- c(1,2,3,4,5,6,6,7,x,9) mean <- mean(data, na.rm = TRUE) print(mean) ```
require(base) + mean(c(6 , 7, 7, 7, 8, 8, 8, 9)) + mean(c(1, 2, NA), na.rm = TRUE) + print
#data data <- c(14, 16, 19, 20, 21, 23) a <- mean(data) print(a)
## Mean of a matrix ```r values <- c(1:9) mat <- matrix(as.vector(values), nrow=3, ncol=3) av<-mean(mat) print(av) ```
 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)) ```
**Mean** ```r fit = (1,3,5,7) mean_fit = mean(fit) print(mean_fit) ```
**Sd** ```r data <- c(2,4,6,3) var <- sd(var) print(data) ```
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) ```
```r data <- c(12, 14, 15, 20, 23, 30) a <- mean(data) print(a) ```