0% found this document useful (0 votes)
95 views7 pages

Measure of Central Tendency Practical

This document discusses various measures of central tendency (mean, median, mode) in R programming language. It provides code examples to calculate the mean, median and mode of different datasets. The mean, median and mode of the age variable in a diabetes dataset are calculated as 33.24, 29 and 44 respectively for single mode data and 23, 44 for multiple mode data. Functions like mean(), median() and modest package are used to find these measures of central tendency in R.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views7 pages

Measure of Central Tendency Practical

This document discusses various measures of central tendency (mean, median, mode) in R programming language. It provides code examples to calculate the mean, median and mode of different datasets. The mean, median and mode of the age variable in a diabetes dataset are calculated as 33.24, 29 and 44 respectively for single mode data and 23, 44 for multiple mode data. Functions like mean(), median() and modest package are used to find these measures of central tendency in R.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical: Measures of central tendency

# Defining vector
x <- c(3, 7, 5, 13, 20, 23, 39, 23, 40, 23, 14, 12, 56, 23)

# Print mean
print(mean(x))
Output:
[1] 21.5

# Defining vector
x <- c(3, 7, 5, 13, 20, 23, 39, 
       23, 40, 23, 14, 12, 56, 23)
  
# Print Median
median(x)

Output:
[1] 21.5

Example 1: Single-mode value


In R language, there is no function to calculate mode. So, modifying
the code to find out the mode for a given set of values.

# Defining vector
x <- c(3, 7, 5, 13, 20, 23, 39,
23, 40, 23, 14, 12, 56,
23, 29, 56, 37, 45, 1, 25, 8)

# Generate frequency table


y <- table(x)

# Print frequency table


print(y)

# Mode of x
m <- names(y)[which(y == max(y))]

# Print mode
print(m)

Output:
x
1 3 5 7 8 12 13 14 20 23 25 29 37 39 40 45 56
1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 2
[1] "23"

Example 2: Multiple Mode values

# Defining vector
x <- c(3, 7, 5, 13, 20, 23, 39, 23, 40,
23, 14, 12, 56, 23, 29, 56, 37,
45, 1, 25, 8, 56, 56)

# Generate frequency table


y <- table(x)

# Print frequency table


print(y)

# Mode of x
m <- names(y)[which(y == max(y))]

# Print mode
print(m)

Output:
x
1 3 5 7 8 12 13 14 20 23 25 29 37 39 40 45 56
1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 4
[1] "23" "56"

# 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))

Mean in R Programming Language


# R program to illustrate
# Descriptive Analysis

# Import the data using read.csv()


myData = read.csv("CardioGoodFitness.csv",
stringsAsFactors=F)

# Compute the mean value


mean = mean(myData$Age)
print(mean)

Output: 
[1] 28.78889

Median in R Programming Language


Syntax: median(x, na.rm = False)
Where, X is a vector and na.rm is used to remove missing value

# 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

Mode in R Programming Language

Using Modest Package

We can use the modest package of the R. This package provides


methods to find the mode of the univariate data and the mode of
the usual probability distribution.
Example: 
# R program to illustrate
# Descriptive Analysis
# Import the library
library(modest)

# Import the data using read.csv()


myData = read.csv("CardioGoodFitness.csv",
stringsAsFactors=F)

# Compute the mode value


mode = mfv(myData$Age)
print(mode)

Output: 
[1] 25

Calculate the mean and median of age in the diabetes dataset.

Output:
OUTPUT: MEAN
[1] 33.24089
OUTPUT: MEDIAN
[1] 29

Calculate the median of age in the diabetes dataset (single


mode,bimode)
Single mode value
OUTPUT:
[1] "44"
Multiple mode values
OUTPUT:
[1] "23" "44"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy