Mean function in R: Mean()

Mean function in R -mean()  calculates the arithmetic mean. mean() function calculates arithmetic mean of vector with NA values  and arithmetic mean of column in data frame. mean of a group can also calculated using mean() function in R by providing it inside the aggregate function. with mean() function we can also perform row wise mean using dplyr package and also column wise mean lets see an example of each.

  • mean of the list of vector elements with NA values
  • mean of a particular column of the dataframe in R
  • Mean of multiple columns of a dataframe in R
  • column wise mean of the dataframe using mean() function
  • mean of the group in R dataframe using aggregate() and dplyr package
  • Row wise mean of the dataframe in R using mean() function

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)
mean(x)
output:
[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)
mean(x,na.rm=TRUE)
output:
[1] 0.4105

 

Example of mean() function in R dataframe: 

Lets create the data frame to demonstrate mean function – mean() in r

### create the dataframe 
my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120),
                       Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3))
my_basket

so the resultant dataframe will be

sum function in R 1

 

mean of a column in R data frame using mean() function :

Mean() function in R 22

mean() function takes the column name as argument and calculates the mean of that particular column

# mean() function in R : mean of a column in data frame

mean(my_basket$Price)

so the resultant mean of “Price” column will be

output:

[1] 67.5

column wise mean using mean() function:

mean() function is applied to the required column through mapply() function, so that it  calculates the mean of required column as shown below.

 
# mean() function in R : mean of multiple column in data frame 

 mapply(mean,my_basket[,c(-1,-2)])

so the resultant mean of “Price” and “Tax” columns will be

Mean Function in R 11

 

 

Mean of the column by group using mean() function

aggregate() function along with the mean() function calculates the mean of a group. here mean of “Price” column, for “Item_Group” is calculated.

##### Mean of the column by group 
aggregate(x= my_basket$Price,
          by= list(my_basket$ITEM_GROUP),
          FUN=mean)

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. mean of price for each group is calculated as shown below

Mean Function in R 12

 

 

Mean of the column by group  and populate it by using mean() function:

group_by() function along with the mean() function calculates the mean of a group. here mean of “Price” column, for “Item_Group” is calculated and populated across as shown below

#### mean of the column by group and populate it using dplyr

library(dplyr)

my_basket %>%
  group_by(ITEM_GROUP) %>%
  mutate(mean_by_group = mean(Price))

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. mean of price for each group is calculated and populated as shown below

Mean Function in R 13

 

 

 

Row wise mean using mean() function along with dplyr

Row wise mean is calculated with the help rowwise() function of dplyr package  and mean() function as shown below

## row wise mean using dplyr 
library(dplyr)

my_basket %>%
  rowwise() %>%
  mutate(
    Mean_price = mean(c(Price,Tax))
  )

row wise mean of “Price” and “Tax” is calculated and  populated for each row as shown below

Mean Function in R 14

 

For further understanding of mean() function in R using dplyr one can refer the dplyr documentation


Related Topics:

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

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.