# R Mean, Median & Mode - r - learn r - r programming

• R uses many in-built functions to perform Statistical analysis in R is performed by using many in-built functions.
• Most of these functions are part of the R base package.
• These functions take R vector as an input along with the arguments and give the result.
• Here we discussing mean, median and mode.

## Mean

• It is calculated by taking the sum of the values and dividing with the number of values in a data series.
• The function mean() is used to calculate this in R.

## Syntax

• The basic syntax for calculating mean in R is
• x is the input vector.
• trim is used to drop some observations from both end of the sorted vector.
• r programming median

• na.rm is used to remove the missing values from the input vector.

## Example

• When we execute the above code, it produces the following result

## Applying Trim Option

• When trim parameter is supplied, the values in the vector get sorted and then the required numbers of observations are dropped from calculating the mean.
• When trim = 0.3, 3 values from each end will be dropped from the calculations to find mean.
• In this case the sorted vector is (−21, −5, 2, 3, 4.2, 7, 8, 12, 18, 54) and the values removed from the vector for calculating mean are (−21,−5,2) from left and (12,18,54) from right.
• When we execute the above code, it produces the following result

## Applying NA Option

• If there are missing values, then the mean function returns NA.
• To drop the missing values from the calculation use na.rm = TRUE. which means remove the NA values.
• When we execute the above code, it produces the following result

## Median

• The middle most value in a data series is called the median. The median()function is used in R to calculate this value.

## Syntax

• The basic syntax for calculating median in R is
• x is the input vector.
• na.rm is used to remove the missing values from the input vector.

## Example

• When we execute the above code, it produces the following result

## Mode

• The mode is the value that has highest number of occurrences in a set of data.
• Unike mean and median, mode can have both numeric and character data.
• R does not have a standard in-built function to calculate mode. So we create a user function to calculate mode of a data set in R.
• This function takes the vector as input and gives the mode value as output.

## Example

• When we execute the above code, it produces the following result