Easy Tutorial
❮ R Matrix R Basic Operators ❯

R mean() Function - Calculate Mean

R Language Examples

The R mean() function is used to calculate the mean of a sample, and the second parameter of the function can be set to remove some outlier data.

The syntax for the mean() function is as follows:

mean(x, trim = 0, na.rm = FALSE, ...)

Parameter Description:

Example

# Create a vector
x <- c(12, 27, 3, 4.2, 2, 2, 54, -21, 4, -2)

# Calculate mean
result.mean <- mean(x)
print(result.mean)

Executing the above code outputs the result:

[1] 8.52

Next, we use the trim parameter to remove some outliers. In the following example, we set trim = 0.3, which will remove 20*0.3=6 data points from both ends of the vector, specifically (1, 2, 3) from the left and (18, 19, 20) from the right.

Example

# Create a vector
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
# Calculate mean
result.mean <- mean(x, trim = 0.3)
result.mean2 <- mean(c(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17))
print(result.mean)
print(result.mean2)

In the above example, mean(x, trim = 0.3) removes 3 elements from each end, which is equivalent to mean(c(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)).

Executing the above code outputs the result:

[1] 10.5
[1] 10.5

In the input vector of the mean function, if an element has no value, it defaults to NA. We can set whether to remove the default NA value through the third parameter. If NA is not removed, the result will be NA:

Example

# Create a vector
x <- c(1, 2, 3, 4.5, 6, NA)

# Calculate mean
result.mean <- mean(x)
print(result.mean)

# Remove NA
result.mean <- mean(x, na.rm = TRUE)
print(result.mean)

Executing the above code outputs the result:

[1] NA
[1] 3.3

R Language Examples

❮ R Matrix R Basic Operators ❯