R(22), Mean, Median, and Mode

Published: by Creative Commons Licence

Mean

Generic function for the (trimmed) arithmetic mean.

mean(x, trim = 0, na.rm = FALSE, ...)
  • x: the input vector
  • trim: the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed. Values of trim outside that range are taken as the nearest endpoint.
  • na.rm: a logical value indicating whether NA values should be stripped before the computation proceeds.

Median

Compute the sample median.

median(x, na.rm = FALSE)
  • x: an object for which a method has been defined, or a numeric vector containing the values whose median is to be computed.
  • na.rm: a logical value indicating whether NA values should be stripped before the computation proceeds.

Mode

A mode is the most frequently occurring value in a set of data.

R language does not have standard built-in functions to compute mode. Therefore, we create a user function to calculate the schema of the dataset in R. This function takes a vector as input and a mode value as output.

# Create the function.
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}

# Create the vector with numbers.
v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

# Calculate the mode using the user function.
result <- getmode(v)
print(result)

# Create the vector with characters.
charv <- c("o","it","the","it","it")

# Calculate the mode using the user function.
result <- getmode(charv)
print(result)

result:

[1] 2
[1] "it"