Example 2: For One Column of Data Frame
Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
Step 2: Create dataset for For One Column 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 variance
weighted_var <- wtd.var(df$points, wt)
Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
Code
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
#Step 2: Create dataset for For One Column 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 variance
weighted_var <- wtd.var(df$points, wt)
#Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
Output
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
Contact Us