Calculate the Weighted Mean in R Programming – weighted.mean() Function
weighted.mean()
function in R Language is used to compute the weighted arithmetic mean of input vector values.
Syntax: weighted.mean(x, weights)
Parameters:
x: data input vector
weights: It is weight of input data.
Returns: weighted mean of given values
Example 1:
x1 <- c (1, 2, 7, 5, 3, 2, 5, 4)
w1 <- c (7, 5, 3, 5, 7, 1, 3, 7)
weighted.mean (x1, w1)
|
Output:
[1] 3.394737
Example 2:
x1 <- c (1, 2, 7, 5, 3, 2, 5, 4)
w1 <- c (7, 5, 3, 8, 7, 1, 3, 7)
x2 <- c (x1, NA )
w2 <- c (w1, 3)
weighted.mean (x2, w2)
weighted.mean (x2, w2, na.rm = TRUE )
|
Output:
[1] NA
[1] 3.512195
Last Updated :
23 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...