youtube video link with explanations for these examples https://youtu.be/eNb15pQYwlI

What is weighted mean

# What is weighted mean


x <- c(10,20,30)
mean(x)
FALSE [1] 20
y <-c(.5, .2, .3 )
weighted.mean(x, w= y)
FALSE [1] 18
# Practical example with real data

library(dplyr)

data <- data.frame(Student =  c( 'A', 'B', 'C' )
                   , GA   =   c( 89,  77,   94 )
                   , Maths =  c( 90,  92,   84 )
                   , Science= c( 80,  78,   82 )
                  )

library(reshape2)
data.melt <- melt(data= data, id = 'Student')


#Create a weights table
weight <- data.frame(variable = c('GA','Maths','Science')
                     ,Wt  = c(80,30,30))
weight
datawt <- merge(data.melt,weight , by = 'variable')
datawt
dm <- datawt%>%
      dplyr::group_by(Student)%>%
      dplyr::summarise(mean = mean(value), wmean =weighted.mean(value, Wt))

dm
# Compare the mean and weighted mean

library(ggplot2)

# Dot plot
pl <- ggplot(data = dm, aes(x = Student, y = mean))
pl <- pl +  geom_bar(stat= "identity")
pl <- pl +  geom_point(aes(x= Student, y = wmean), color = "red")
pl <- pl +  theme_classic()
pl

# Bar plot
data.plot <- melt(data= dm, id = 'Student')
pl <- ggplot(data = data.plot, aes(x = Student, y = value, fill= variable))
pl <- pl +  geom_bar(stat= "identity", position = position_dodge())
pl <- pl +  theme_classic()
pl <- pl + labs(fill = "Calculation")
pl