# Weighted Mean in R (5 Examples)

This tutorial explains how to compute the weighted mean in the R programming language.

The tutorial is mainly based on the weighted.mean() function. So let’s have a look at the basic R syntax and the definition of the weighted.mean function first:

Basic R Syntax of weighted.mean():

`weighted.mean(x, weights)`

Definition of weighted.mean():

The weighted.mean function computes the weighted arithmetic mean of a numeric input vector.

You are here for the answer, so let’s move on to the examples!

## Example 1: Basic Application of weighted.mean Function in R

First, we need to create some example data and a vector with corresponding weights. Consider the following example data:

```x1 <- c(9, 5, 2, 7, 3, 6, 4, 5) # Create example data w1 <- c(2, 3, 1, 5, 7, 1, 3, 7) # Create example weights```

Now, we can use the weighted.mean command to compute the weighted mean of this data:

```weighted.mean(x1, w1) # Apply weighted.mean function # 4.965517```

As you can see based on the RStudio console output, the weighted mean of our data is 4.965517.

## Example 2: Handle NAs with weighted.mean Function

A typical problem is the occurrence of missing values in our data (i.e. NA values). Let’s extent our example data with NA values to simulate this situation:

```x2 <- c(x1, NA) # Create vector with NA w2 <- c(w1, 3) # Extend weights vector```

If we now apply the weighted.mean function as in Example 1, the RStudio console returns NA:

```weighted.mean(x2, w2) # weighted.mean with NA # NA```

Fortunately, we can easily ignore NA values in our data with the na.rm option of the weighted.mean function:

```weighted.mean(x2, w2, na.rm = TRUE) # Remove missing values # 4.965517```

## Example 3: Compute Weighted Means by Group

Often we are only interested to know the weighted mean for a subgroup of our data. Consider the following example data:

```group <- c("A", "B", "A", "C", "C", "A", "B", "B") # Create group indicator data <- data.frame(x1, w1, group) # Create data frame``` Table 1: Example Data with Numeric Column, Weights & Group Indicator.

To compute the weighted mean by group we can use the functions of the dplyr package. Let’s install and load the package to R:

```install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package```

Now, we can calculate the weighted mean with the following R code:

```data %>% # Weighted mean by group group_by(group) %>% summarise(weighted.mean(x1, w1))``` Figure 1: dplyr Tibble Containing Weighted Means.

As you can see based on Figure 1, the previous R code returns a tibble with the weighted means by group to the RStudio console.

## Alternative 1: weightedMean Function of matrixStats Package

The beauty of R is that there are always several ways to achieve a goal. Of cause, this is also true for the computation of the weighted mean. In the following two examples, I will therefore show some alternatives to the weighted.mean function.

Let’s start with the weightedMean function of the matrixStats package. First, we need to install and load the package…

```install.packages("matrixStats") # Install matrixStats package library("matrixStats") # Load matrixStats package```

…and then we can apply the weightedMean R function as follows:

```weightedMean(x1, w1) # Apply weightedMean function # 4.965517```

The same result as in Example 1 – looks good!

## Alternative 2: wt.mean Function of SDMTools Package

Another alternative is the wt.mean function of the SDMTools package:

```install.packages("SDMTools") # Install SDMTools package library("SDMTools") # Load SDMTools package```

After installing and loading the R package, we can apply the wt.mean command as follows:

```wt.mean(x1, w1) # Apply wt.mean function # 4.965517```

Again, the same result.

## Further Resources & Summary

This article illustrated how to compute weighted means in the R programming language. In case you want to learn more the theoretical research concept of the weighted arithmetic mean, I can recommend the following video tutorial of the MySecretMathTutor YouTube channel:

In addition, you may also have a look at some of the related R tutorials that I have published on this website:

I hope this tutorial contained the content you were looking for. However, don’t hesitate to let me know in case you have any comments or questions.