To compute a weighted average in R, you can use the weighted.mean()
function, which takes two arguments: x
for the values you want to compute the weighted average for, and w
for the weights of each value.
Here is an example of how to use weighted.mean()
:
main.r206 chars7 lines
This will return the weighted average of the values
vector using the weights
vector, which is 6.9
.
You can also specify the na.rm
argument to remove any NA
values before computing the weighted average:
main.r268 chars9 lines
gistlibby LogSnag