在 R 编程中计算算术平均值 – mean()函数
R 语言中的mean()
函数用于计算作为参数传递给它的数字向量元素的算术平均值。
Syntax: mean(x, na.rm)
Parameters:
x: Numeric Vector
na.rm: Boolean value to ignore NA value
示例 1:
# R program to calculate
# Arithmetic mean of a vector
# Creating a numeric vector
x1 <- c(1, 2, 3, 4, 5, 6)
x2 <-c(1.2, 2.3, 3.4, 4.5)
x3 <- c(-1, -2, -3, -4, -5, -6)
# Calling mean() function
mean(x1)
mean(x2)
mean(x3)
输出:
[1] 3.5
[1] 2.85
[1] -3.5
示例 2:
# R program to calculate
# Arithmetic mean of a vector
# Creating a numeric vector
x1 <- c(1, 2, 3, NA, 5, NA, NA)
# Calling mean() function
# including NA values
mean(x1, na.rm = FALSE)
# Calling mean() function
# excluding NA values
mean(x1, na.rm = TRUE)
输出:
[1] NA
[1] 2.75