Median in R Programming Language
It is the middle value of the data set. It splits the data into two halves. If the number of elements in the data set is odd then the center element is median and if it is even then the median would be the average of two central elements.
Where n = number of terms
Syntax: median(x, na.rm = False)
Where, X is a vector and na.rm is used to remove missing value
Example:
R
# R program to illustrate # Descriptive Analysis # Import the data using read.csv() myData = read.csv ( "CardioGoodFitness.csv" , stringsAsFactors=F) # Compute the median value median = median (myData$Age) print (median) |
Output:
[1] 26
Mean, Median and Mode in R Programming
The measure of central tendency in R Language represents the whole set of data by a single value. It gives us the location of the central points. There are three main measures of central tendency:
- Mean
- Median
- Mode
Prerequisite:
Before doing any computation, first of all, we need to prepare our data and save our data in external .txt or .csv files and it’s a best practice to save the file in the current directory. After that import, your data into R as follow:
R
# R program to import data into R # Import the data using read.csv() myData = read.csv ( "CardioGoodFitness.csv" , stringsAsFactors=F) # Print the first 6 rows print ( head (myData)) |
Output:
Product Age Gender Education MaritalStatus Usage Fitness Income Miles 1 TM195 18 Male 14 Single 3 4 29562 112 2 TM195 19 Male 15 Single 2 3 31836 75 3 TM195 19 Female 14 Partnered 4 3 30699 66 4 TM195 19 Male 12 Single 3 3 32973 85 5 TM195 20 Male 13 Partnered 4 2 35247 47 6 TM195 20 Female 14 Partnered 3 3 32973 66