In R, the median()
method is used to find the median of the sample data, with the assumption that the dataset or sample data is sorted from smallest to largest.
The syntax for the median()
function to calculate the middle value of the vector x
is as follows:
median(x, na.rm = FALSE,...)
x
: vector of numerical values.na.rm
: default=False. na.rm
is a boolean field that can either be True
or False
. If it is set to False
, the dataset does not contain NA
values. Otherwise, we can set it to True
....
: further potential arguments for methods.x
.x
is an even-length integer, the return type will be double.na.rm
is set to False
.The code snippet below illustrates how the median()
method works with a single argument. It takes x
to compute the median of these values.
# Create the vector.x <- c(1,2,3,4,5.3,6,7,8,9,13,11)# Find the median.mid_value <- median(x)cat("Median is:", mid_value)
In the example below, the median()
method takes a vector x
and na.rm
set to TRUE
. median()
will first eliminate NA values and then it will compute the median of the remaining values.
# Create the vector.x <- c(1,2,3,4,5.3,6,7,8,9,NA,13,11)# Find the median.mid_value <- median(x,na.rm=TRUE)print(mid_value)