Mean function in R: Calculate arithmetic mean

Mean function in R -mean()  calculates the arithmetic mean. lets see an example to calculate arithmetic mean of vector with NA values  and arithmetic mean of column in data frame

Syntax for mean() function in R:

mean(x, na.rm = FALSE, …)
  • x – numeric vector
  • rm- whether NA should be removed, if not, NA will be returned

Example of R Mean() function:

# R mean function

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
[1] 0.4105


Example of R Mean() function with NA:

Mean function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in mean() function

# mean function for input vector which has NA.

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
[1] 0.4105


Example of R Mean() function for a column in data frame:

Lets use mtcars data frame to demonstrate mean function in r

# R mean function - for column in data frame

[1] 20.09062


previous small mean function in r                                                                                                          next small mean function in r