Calculate mean of multiple columns of R DataFrame Last Updated : 09 May, 2021 Comments Improve Suggest changes Like Article Like Report Mean is a numerical representation of the central tendency of the sample in consideration. In this article, we are going to calculate the mean of multiple columns of a dataframe in R Programming Language. Formula: Mean= sum of observations/total number of observations. Method 1: Using colMeans() function colMeans() this will return the column-wise mean of the given dataframe. Syntax: colMeans(dataframe_name) where dataframe_name is the input dataframe. For this simply pass the dataframe in use to the colMeans() function. The result will be the mean of all the individual columns. Example: R # create a vector 1 a=c(1,2,3,4,5) # create a vector 2 b=c(3,5,6,7,3) # create a vector 3 d=c(34,56,78,32,45) # pass these vectors to # dataframe data=data.frame(a,b,d) print(data) # mean columns of the # dataframe print(colMeans(data)) Output: Example 2: R # create a vector 1 a=c(1,2,3,4,5) # pass the vector to data frame data=data.frame(a) print(data) # mean column of the dataframe print(colMeans(data)) Output: Method 2: Using sapply() function Syntax: sapply(dataframe,mean) where dataframe is the input dataframe and mean is the method to calculate mean. For this dataframe name needs to be passed along with the action to perform in our case mean. Example: R # numeric vector a a=c(1,2,3,4,5) # numeric vector b b=c(3,4,5,6,7) # numeric vector d d=c(34.6,78.8,89,9.43,67.9) # pass the vectors to data frame data=data.frame(a,b,d) # use sapply function to calculate mean print(sapply(data,mean)) Output: a b d 3.000 5.000 55.946 Example 2 R # numeric vector a a=c(1,2,3,4,5) # pass the vector to data frame data=data.frame(a) # use sapply function to calculate mean print(sapply(data,mean)) Output: a 3 Comment More infoAdvertise with us Next Article Calculate mean of multiple columns of R DataFrame M manojkumarreddymallidi Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs Similar Reads Sum of Two or Multiple DataFrame Columns in R In this article, we will discuss how to perform some of two and multiple dataframes columns in R programming language. Database in use: Sum of two columns The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using 2 min read Calculate difference between columns of R DataFrame Generally, the difference between two columns can be calculated from a dataframe that contains some numeric data. In this article, we will discuss how the difference between columns can be calculated in the R programming language. Approach Create a dataframe and the columns should be of numeric or i 2 min read How to calculate the mode of all rows or columns from a dataframe in R ? In this article, we will discuss how to calculate the mode of all rows and columns from a dataframe in R Programming Language. Method 1: Using DescTools package The DescTools package in R is used to perform descriptive analysis. It contains a collection of miscellaneous basic statistic functions and 4 min read Split DataFrame Variable into Multiple Columns in R In this article, we will discuss how to split dataframe variables into multiple columns using R programming language. Method 1: Using do.call method The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into 3 min read How to get the classes of all columns in a dataframe in R ? In this article, we will discuss how to find all the classes of the dataframe in R Programming Language. There are two methods to find the classes of columns in the dataframe. Using str() functionUsing lapply() function Method1 : Using str() function This function will return the class and value of 2 min read Like