在 R 编程中计算向量元素的中位数 – 中位数()函数
R 语言中的median()
函数用于计算作为参数传递的数值向量的元素的中位数。
Syntax: median(x, na.rm)
Parameters:
x: Numeric Vector
na.rm: Boolean value to ignore NA
示例 1:
# R program to calculate median of a vector
# Creating vector
x1 <- c(2, 3, 5, 7, 4, 8, 6)
x2 <- c(-3, -5, 6, 2, -4)
# Calling median() function
median(x1)
median(x2)
输出:
[1] 5
[1] -3
示例 2:
# R program to calculate median of a vector
# Creating vector
x <- c(2, 3, 5, NA, 4, NA, 6)
# Calling median() function
# with NA values included
median(x, na.rm = FALSE)
# Calling median() function
# with NA values excluded
median(x, na.rm = TRUE)
输出:
[1] NA
[1] 4