How to Calculate Weighted Standard Deviation in R
Last Updated :
26 Mar, 2024
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
Example 1: For One Vector
Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
Step 2: Create Dataset of 1 vector
x <- c(14, 19, 22, 25, 29, 31, 31, 38, 40, 41)
Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2, 3, 2)
Step 4: Calculate weighted variance
weighted_var <- wtd.var(x, wt)
Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
R
#Step 1: Install Package
install.packages("Hmisc")
library(Hmisc)
#Step 2: Create Dataset of 1 vector
x <- c(14, 19, 22, 25, 29, 31, 31, 38, 40, 41)
#Step 3: Define weights
wt <- c(1, 1, 1.5, 2, 2, 1.5, 1, 2, 3, 2)
#Step 4: Calculate weighted variance
weighted_var <- wtd.var(x, wt)
#Step 5: Calculate weighted standard deviation
sqrt(weighted_var)
Output
8.570051
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
R
#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
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
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.
Similar Reads
How To Calculate Standard Deviation in MATLAB?
Standard deviation is a statistical quantity that tells us how much-distributed data is from its mean value. Standard deviation can also be defined as the square root of the Variance of the same data. Standard Deviation in MATLAB:MATLAB provides a simple function to calculate the standard deviation
2 min read
How to Calculate Standard Deviation?
Standard Deviation is a measure of how data is spread out around the mean. It is a statistical tool used to determine the amount of variation or dispersion of a set of values from the mean. A low standard deviation indicates that the data points are clustered closely around the mean, while a high st
7 min read
How to Calculate Pooled Standard Deviation in R
In this article, we will learn How to Calculate Pooled Standard Deviation in R. Pooled Standard DeviationThis is a weighted average of standard deviations of two or more groups which are independent. In statistics, it is used to test whether or not the means of two populations are equal. Mathematica
2 min read
How to Calculate Relative Standard Deviation in Excel?
Definition: Related standard deviation is also known as the relative percentage standard deviation form, the deviation measurement which tells us how different numbers are dispersed around the mean in a particular set of data. This format shows the percentage distribution of data. If a relative stan
2 min read
How to Calculate Standard Deviation in Excel: Quick Guide
How to Find Standard Deviation in ExcelOpen MS Excel Spreadsheet >> Enter your Data Select the Cell >>Enter the Formula For a sample: =STDEV.S(range)For a population: =STDEV.P(range)Press Enter >>Verify your Result Calculating the standard deviation in Excel is a crucial step in da
9 min read
How to Calculate Weighted MAPE in Excel?
In statistics, we often use Forecasting Accuracy which denotes the closeness of a quantity to the actual value of that particular quantity. The actual value is also known as the true value. It basically denotes the degree of closeness or a verification process that is highly used by business profess
3 min read
How to Calculate Weighted Average in Pandas?
A weighted average is a computation that considers the relative value of the integers in a data collection. Each value in the data set is scaled by a predefined weight before the final computation is completed when computing a weighted average. Syntax: def weighted_average(dataframe, value, weight):
3 min read
How to Find Standard Deviation in R?
In this article, we will discuss how to find the Standard Deviation in R Programming Language. Standard deviation R is the measure of the dispersion of the values. It can also be defined as the square root of variance. Formula of sample standard deviation: s = \sqrt{\frac{1}{N-1}\displaystyle\sum\li
2 min read
How to Calculate Mean using Step Deviation Method?
Step Deviation Method is a simplified way to calculate the mean of a grouped frequency distribution, especially when the class intervals are uniform. In simple words, statistics implies the process of gathering, sorting, examining, interpreting and then understandably presenting the data to enable o
6 min read
How to calculate the Weighted Mean?
Answer: Let's consider the data points x1,x2,x3,...,xn which are associated with weights w1,w2,w3,...,wn then the weighted mean can be calculated by the formulaWeighted Mean = âin=1 xi.wi/âin=1 wi=(x1w1+x2w2+x3w3+...+xnwn)/(w1+w2+w3+...+wn)The weighted mean is a type of average where each value in a
5 min read