Harmonic Mean in R (Harmonic mean of column in R)

Harmonic mean of the column in R is calculated using harmonic.mean() function of the psych package. Let’s see how to calculate the harmonic mean of the dataframe in R and harmonic mean of the vector with an example of each.

Let’s first create the dataframe

install.packages("psych")
library(psych)

df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                 Mathematics_score=c(45,78,44,89,66,49,72),
                 Science_score=c(56,52,45,88,33,90,47))

So the resultant dataframe will be

Harmonic Mean in R 1 harmonic mean of column in R 1

 

Harmonic mean of the column in R:

Harmonic mean of the Mathematics_score is calculated as shown below

  harmonic.mean(df1$Mathematics_score,na.rm=TRUE)

So the Harmonic mean of the column will be

[1] 59.09441

 

Harmonic mean of the vector in R:

vector1 <- c(50,47,45,89,66,49,72)  # create vector
harmonic.mean(vector1,na.rm=TRUE)  

So the Harmonic mean of the vector will be,

[1] 56.36827

 

                                                                                                     

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