Get Median of a column in R

Median of a column in R can be calculated by using median() function. Let’s see how to calculate Median 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 median of a column in R 1

 

Get Median of the column in R

Method 1:

Get Median of the column by column name

# Get Median of the column by column name

median(df1$Mathematics1_score)

Result:

[1] 66

 

Method 2:

Get Median of the column by column position

# Get Median of the column by column position

median(df1[,3])

Result:

[1] 66

 

                                                                                               

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.

    View all posts