R provides the weighted.mean() function to calculate the weighted mean. It takes two parameters: a vector of values and a vector of corresponding weights. The function multiplies each value by its weight, sums up the products, and then divides by the sum of the weights.
Example code:
# Create a vector of values and weights
values <- c(10, 20, 30)
weights <- c(0.2, 0.3, 0.5)
# Calculate the weighted mean
weighted_mean <- weighted.mean(values, weights)
print(weighted_mean)
Output:
[1] 23