R Mean and Median

We can easily find the mean and median values from a vector or set or dataframe using the mean() and median() functions respectively. Let us learn about them with examples:

R mean

To get the mean of the values in the R programming language, use the mean() method. Let us see an example to calculate the mean:


We can also trim the left and right observations using the trim parameter of the mean() method in R. The vector will be first sorted, then according to the value in the parameter, the values will get trimmed, for example trim = 0.2 would mean trim two values from the left as well right.

Here is an example:


The vector will be sorted first above:

Then, 0.2 means two observations from both the left position will get removed and then the mean gets calculated for the rest of the observations.

R median

To get the median of the values in the R programming language, use the median() method. Median is defined as the middle value of the given list. Let us see an example to calculate the median:


We can also see another example, wherein we can drop the missing values using the na.rm parameter. Let us see an example and first create a vector with NA values:


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now

Read More:

R String Operations
R Min and Max
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading