Home » How to Calculate a Weighted Mean in R

How to Calculate a Weighted Mean in R

by Erma Khan

To calculate a weighted mean in R, you can use the built-in weighted.mean() function, which uses the following syntax:

weighted.mean(x, w)

where:

  • x: A vector of raw data values
  • w: A vector of weights

This tutorial shows several examples of how to use this function in practice.

Example 1: Weighted Mean of a Vector

The following code shows how to calculated the weighted mean for a given vector of data:

#define vector of data values
data #define vector of weights
weights #calculate weighted mean
weighted.mean(x=data, w=weights)

[1] 5.8

The weighted mean turns out to be 5.8.

Example 2: Weighted Mean of a Column in a Data Frame

The following code shows how to calculated the weighted mean for a column in a data frame, using another column as the weights:

#create data frame
df #calculate weighted mean
weighted.mean(x=df$values, w=df$weights)
[1] 5.8

The weighted mean turns out to be 5.8.

Note that you can also calculate the weighted mean for a column in a data frame by using a separate vector as the weights:

#create data frame
df #define vector of weights
weights #calculate weighted mean
weighted.mean(x=df$values, w=weights)
[1] 5.8

Once again the weighted mean turns out to be 5.8.

When to Use a Weighted Mean

In practice, a weighted mean is used when we consider some data values to be more important than others and so we want those more important values to contribute more to the final mean.

Additional Resources

How to Calculate the Mean of Multiple Columns in R
How to Calculate the Mean by Group in R
How to Sum Specific Columns in R

Related Posts