# 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.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

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

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.