# Weighted Sum in R (Example)

This article shows in an R programming example how to **compute the weighted sum**.

First, we need to create some example data in R. I’m going to create one numeric vector containing my observed values…

x <- c(8, 5, 1, 7, 10) # Create vector of values

…and another numeric vector containing my weights (in reality this could be sampling weights):

w <- c(5, 8, 15, 1, 7) # Create vector of weights

In order to calculate the weighted sum of our data, we can apply the sum R function to the product of x and w (i.e. we multiply our observed values with our weights and then add all values):

sum(x * w) # Compute weighted sum # 172

The RStudio console is then showing the result of our calculation: The weighted sum of our example data is 172.

## Further Resources & Summary

Do you want to learn more about the sum function that we have used in the previous example? Then you could have a look at the following video of my YouTube channel. In the video, I’m explaining the application of the sum function in R in more detail.

Furthermore, you could have a look at the other R tutorials on my website. Below, you can find a list of useful R tutorials:

- Sum in R
- Sum by Group in R
- Weighted Mean in R
- Column & Row Sums & Means
- The cumsum Function in R
- R Functions List (+ Examples)
- The R Programming Language

In this R tutorial you learned how to compute the weighted sum of a numeric vector. If you have any further questions or comments on the previously shown example, don’t hesitate to let me know in the comments section below.

In addition, you could subscribe to my email newsletter in order to get regular updates on new tutorials. You can subscribe to the newsletter in the box below.