Get Mean of a column in R

Mean of a column in R can be calculated by using mean() function. Let’s see how to calculate Mean in R with an example.

Let’s first create the dataframe.

### Create Data Frame
df1 = 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))
df1

So the resultant dataframe will be

Get mean of a column in R 1

 

Get Mean of the column in R

Method 1:

Get Mean of the column by column name

# Get Mean of the column by column name

mean(df1$Mathematics1_score)

Result:

[1] 63.28571

Method 2:

Get Mean of the column by column position

# Get Mean of the column by column position

mean(df1[,3])

Result:

[1] 63.28571