R Programming language - R mean, median and mode
In R, the mean()
, median()
, and mode()
functions are used to calculate the statistical measures of central tendency.
The mean()
function returns the average value of a vector or set of values, while the median()
function returns the middle value in a vector or set of values. The mode()
function returns the most frequently occurring value in a vector or set of values.
Here are some examples of how to use these functions in R:
re refto:theitroad.com# Create a vector of numbers x <- c(5, 2, 8, 4, 1) # Calculate the mean of the vector mean(x) # Output: 4 # Calculate the median of the vector median(x) # Output: 4 # Calculate the mode of the vector library(modeest) mfv(x) # Output: 5 # Create another vector of numbers y <- c(5, 2, 8, 4, 1, 4) # Calculate the mean of the vector mean(y) # Output: 4 # Calculate the median of the vector median(y) # Output: 4 # Calculate the mode of the vector mfv(y) # Output: 4
Note that the modeest
library is required to use the mode()
function in R.
In addition to vectors and sets of numbers, you can also use these functions on other R data structures such as matrices, data frames, and lists. In these cases, you can specify which column or element to find the mean, median, or mode of.