Example 3: For Multiple Columns of Data Frame

Step 1: Install Package

install.packages("Hmisc")
library(Hmisc)

Step 2: Create dataset for For Multiple Columns of Data Frame

df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
wins=c(2, 9, 11, 12, 15, 17, 18, 19),
points=c(1, 2, 2, 2, 3, 3, 3, 3))

Step 3: Define weights

wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

Step 4: Calculate weighted standard deviation of points and wins

sapply(df[c('wins', 'points')], function(x) sqrt(wtd.var(x, wt)))

Code

R
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)

#Step 2: Create dataset for For Multiple Columns of Data Frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
                 wins=c(2, 9, 11, 12, 15, 17, 18, 19),
                 points=c(1, 2, 2, 2, 3, 3, 3, 3))

#Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2)

#Step 4: Calculate weighted standard deviation of points and wins
sapply(df[c('wins', 'points')], function(x) sqrt(wtd.var(x, wt)))


Output

  wins                        points 
4.9535723 0.6727938

How to Calculate Weighted Standard Deviation in R

The weighted standard deviation is a method to measure the dispersion of values in a dataset when some values in the dataset have higher values than others.

Mathematically, it is defined as:

where:

  • N: The total number of observations
  • M: The number of non-zero weights
  • wi: A vector of weights
  • xi: A vector of data values
  • x: The weighted mean

We can use wt.var() function from the Hmisc package to Calculate Weighted Standard Deviation in R

Similar Reads

Example 1: For One Vector

Step 1: Install Package...

Example 2: For One Column of Data Frame

Step 1: Install Package...

Example 3: For Multiple Columns of Data Frame

Step 1: Install Package...

Conclusion

In this article, we learnt about How to Calculate Weighted Standard Deviation in R. We learnt different examples for calculating Weighted Standard Deviation for One Vector, One Column of Data Frame and for Multiple Columns of Data Frame....

Contact Us