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 vectorweights:It is weight of input data.

Returns:weighted mean of given values

**Example 1:**

`# Create example data` `x1 <- ` `c` `(1, 2, 7, 5, 3, 2, 5, 4)` ` ` `# Create example weights ` `w1 <- ` `c` `(7, 5, 3, 5, 7, 1, 3, 7) ` ` ` `# Apply weighted.mean() function` `weighted.mean` `(x1, w1)` |

**Output:**

[1] 3.394737

**Example 2:**

`# Create example data` `x1 <- ` `c` `(1, 2, 7, 5, 3, 2, 5, 4)` ` ` `# Create example weights ` `w1 <- ` `c` `(7, 5, 3, 8, 7, 1, 3, 7) ` ` ` `# Create vector with NA` `# Extend weights vector` `x2 <- ` `c` `(x1, ` `NA` `) ` `w2 <- ` `c` `(w1, 3) ` `weighted.mean` `(x2, w2) ` ` ` `# Remove missing values` `weighted.mean` `(x2, w2, na.rm = ` `TRUE` `) ` |

**Output:**

[1] NA [1] 3.512195