Median of a column in R deserve to be calculated by utilizing mean() feature. Mean() Function takes column name as debate and also calculates the intend value of that column. Median of single column in R, Typical of multiple columns in R utilizing dplyr. Get row wise mean in R. Let’s watch just how to calculate Median in R with an example

Typical of the single column in R – mean() functionAverage of multiple columns in RTypical of Multiple columns in R making use of dplyrFind Average of the column by column nameFind Median of the column by column positionGet Row wise expect in R

Let’s first develop the dataframe.

You are watching: How to get mean of a column in r

### Create Documents Framedf1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), Grade_score=c(4,6,2,9,5,7,8), Mathematics1_score=c(45,78,44,89,66,49,72), Science_score=c(56,52,45,88,33,90,47))df1So the resultant datastructure will be

*

 

 

Get Mean of the column in R : Average of the columns by its name

*

Method 1: Get Mean of the column by column name

# Get Average of the column by column namemean(df1$Mathematics1_score)Result:


<1> 63.28571

 

 

Get Typical of the column in R: Typical of the columns by position

Method 2: Get Mean of the column by column position

# Get Mean of the column by column positionmean(df1<,3>)Result:


<1> 63.28571

 

 

Get Typical of multiple columns R using colMeans() : Method 1

ColMeans() Function together with sapply() is provided to acquire the expect of the multiple column. Dataframe is passed as an dispute to ColMeans() Function. Mean of numeric columns of the datastructure is calculated.

# Get Typical of the multiple columnscolMeans(df1) Average of numeric columns of the dataframework will certainly be

*

 

 

Get Average of multiple columns utilizing Dplyr : Method 2

summarise_if() Function along with is.numeric is used to acquire the suppose of the multiple column . With the aid of summarise_if() Function, Median of numeric columns of the dataframework is calculated.

See more: God Why Don T You Do Something ? Do Something Song Lyrics

# Get Mean of the multiple columns making use of dplyrlibrary(dplyr)df1 %>% summarise_if(is.numeric, mean)Typical of numeric columns of the datastructure will certainly be

*

 

 

 

Get Row wise intend in R

Let’s calculate the row wise mean of mathematics1_score and also science_score as presented below.using rowMeans() feature which takes matrix as input. so the dataframework is converted to matrix utilizing as.matrix() function.

# Get Row wise intend in Rdf1$Avg_score = rowMeans(df1<,c(3,4)>)df1so the resultant dataframe with row wise mean calculated will be

*