0% found this document useful (0 votes)
6 views112 pages

Programming in R

The document provides an overview of the R programming language, emphasizing its use for statistical computing and data visualization. It covers essential concepts such as variables, data types, data structures, and looping mechanisms in R, along with examples for clarity. Additionally, it discusses data import/export methods and the significance of attributes in data analysis.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views112 pages

Programming in R

The document provides an overview of the R programming language, emphasizing its use for statistical computing and data visualization. It covers essential concepts such as variables, data types, data structures, and looping mechanisms in R, along with examples for clarity. Additionally, it discusses data import/export methods and the significance of attributes in data analysis.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming with R

1
Introduction
•R is a popular programming language used for statistical computing and
graphical presentation.
•Its most common use is to analyze and visualize data.
•It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter plot,
etc++
•It works on different platforms (Windows, Mac, Linux)
•It is open-source and free
•It has a large community support
•It has many packages (libraries of functions) that can be used to solve different
problems.
2
R Variables
A variable is a memory allocated for the storage of specific data and the
name associated with the variable is used to work around this reserved
block.
Syntax

Using equal to operators


variable_name = value

using leftward operator


variable_name <- value

using rightward operator


value -> variable_name
3
Example
# using equal to operator
var1 = "hello"
print(var1)

# using leftward operator


var2 <- "hello"
print(var2)

# using rightward operator


"hello" -> var3
print(var3)
Output
[1] "hello"
[1] "hello"
[1] "hello" 4
Attributes

• Data attributes refer to the specific characteristics or properties that


describe individual data objects within a dataset.
• These attributes provide meaningful information about the objects and
are used to analyze, classify, or manipulate the data
• Attributes can be categorized into four types: nominal, ordinal, Binary
,Numerical (interval, and ratio ).

5
1. Nominal Attributes :

• Nominal attributes, as related to names, refer to categorical data where the values
represent different categories or labels without any inherent order or ranking.
• These attributes are often used to represent names or labels associated with objects,
entities, or concepts.

6
Binary Attributes:
• Binary attributes are a type of qualitative attribute where the data can take on only two distinct values
or states.
• These attributes are often used to represent yes/no, presence/absence, or true/false conditions within a
dataset.

Symmetric:
• In a symmetric attribute, both values or states are considered equally important or
interchangeable.

Asymmetric:
• An asymmetric attribute indicates that the two values or states are not equally important or
interchangeable.
• For instance, in the attribute “Result” with values “Pass” and “Fail,” the states are not of equal
importance; passing may hold greater significance than failing in certain contexts, such as academic
grading or certification exams

7
Ordinal Attributes :
• Ordinal attributes are a type of qualitative attribute where the values possess a meaningful order or ranking, but the
magnitude between values is not precisely quantified.
• In other words, while the order of values indicates their relative importance or precedence, the numerical difference
between them is not standardized or known.
Example:

. Numeric:
A numeric attribute is quantitative because, it is a measurable quantity,
•interval-scaled attribute has values, whose differences are interpretable, but the numerical attributes do not have the
correct reference point, or we can call zero points.
•Example:Temprature
• ratio-scaled attribute is a numeric attribute with a fix zero-point. If a measurement is ratio-scaled, we can say of a
value as being a multiple (or ratio) of another value.
• The values are ordered, and we can also compute the difference between values, and the mean, median, mode,
Quantile-range, and Five number summary can be given.
8
Attributes

9
Numeric, Character, and Logical Data Types

10
Numeric, Character, and Logical Data Types

11
Data Structures
in R

12
The most essential data structures used in R include:
•Scalers
•Vectors
•Lists
•Dataframes
•Matrices
•Arrays
•Factors

13
14
15
Vectors
Vector is one of the basic data structures in R. It is homogenous, which
means that it only contains elements of the same data type. Data types can be
numeric, integer, character, complex, or logical.
Example
# Vectors(ordered collection of same data type)
X = c(1, 3, 5, 7, 8)
# Printing those elements in console
print(X)
Output
[1] 1 3 5 7 8 16
Lists
A list is a non-homogeneous data structure, which implies that it can contain
elements of different data types. It accepts numbers, characters, lists, and even
matrices and functions inside it. It is created by using the list() function.
Example
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)
Output
[[1]] [1] 1 2 3 4
[[2]] [1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]] [1] 4
17
Matrices
A matrix is a rectangular arrangement of numbers in rows and columns. In a
matrix, as we know rows are the ones that run horizontally and columns are the
ones that run vertically. Matrices are two-dimensional, homogeneous data
structures.
Example
M1 <- matrix(c(1:9), nrow = 3, ncol =3, byrow= TRUE)
print(M1)

Output
[,1] [,2] [,3]
[,1] 1 2 3
[,2] 4 5 6
[,3] 7 8 9 18
19
20
21
22
23
Arrays
Arrays refer to the type of data structure that is used to store multiple items of
a similar type together. This leads to a collection of items that are stored at
contiguous memory locations. This memory location is denoted by the array name.
The position of an element can be calculated simply by adding an offset to its base
value.
Example
Array Structure
An array consists of the following:
Array Index: The array index identifies the location of the element. The array index
starts with 0.
Array Element: Array elements are items that are stored in the array.
Array Length: The array length is determined by the number of elements that can
be stored by the array.
24
There are two types of arrays:
•One-dimensional Arrays
•Multi-dimensional Arrays

•One-dimensional Arrays
One- or single-dimensional arrays are the types of arrays that have array elements
stored in a sequence and can be accessed in the same order.
Multi-dimensional Arrays
Multi-dimensional arrays are arrays that have elements stored in more than one
dimension. They can be two- or three-dimensional arrays and can consist of row
and column indexes.
To create an array in R:
The array() function is utilised.
In this function, the input is a vector.
For creating the array, the value in the dim parameter is utilised.
25
For example:
In this following example, we will create an array in R of two 3×3 matrices
each with 3 rows and 3 columns.

# Create two vectors of different lengths.

> vec1 <- c(1,2,4) #Author DataFlair


> vec2 <- c(15,17,27,3,10,11)
> output <- array(c(vec1,vec2),dim = c(3,3,2))
>output

26
Output

27
Data Frames
A data frame is a two-dimensional array-like structure, or we can say it is a table
in which each column contains the value of one variable, and row contains the
set of value from each column.
There are the following characteristics of a data frame:
•The column name will be non-empty.
•The row names will be unique.
•A data frame stores numeric, factor or character type data.
•Each column will contain same number of data items.
To create a data frame we use the [Link]() function.
28
R program to illustrate dataframe
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")

# A vector which is a character vector


Language = c("R", "Python", "Java")

# A vector which is a numeric vector


Age = c(22, 25, 45)

# To create dataframe use [Link] command


# and then pass each of the vectors
# we have created as arguments
# to the function [Link]()
df = [Link](Name, Language, Age)

print(df) 29
R Data Frames Structure
Get the Structure of the R Data Frame
One can get the structure of the R data frame using str() function in R.
Summary of Data in the R data frame
Extract Data from Data Frame in R
Expand Data Frame in R Language
Remove Rows and Columns
Factors
● Factors are also data objects that are used to categorize the data and store
it as levels.
● Factors can store both strings and integers.
● Columns have a limited number of unique values so that factors are very
useful in columns.
● It is very useful in data analysis for statistical modeling.

Factors are created with the help of factor() function by taking a vector as an
input parameter.

36
Example

# Create a vector as input.


data<-
c("East","West","East","North","North","East","West","West","West","East","North"
)
print(data)
print([Link](data))
# Apply the factor function.
factor_data <- factor(data)
print(factor_data) 37
Output
[1] "East" "West" "East" "North" "North" "East" "West" "West" "West" "East" "North"
[1] FALSE
[1] East West East North North East West West West East North
Levels: East North West
[1] TRUE

38
Strings in R
A string is a sequence of characters. For example, "Programming" is a string that
includes characters: P, r, o, g, r, a, m, m, i, n, g.
In R, we represent strings using quotation marks (double quotes, " " or single
quotes, ' ').
For example
# string value using single quotes
'Hello‘
# string value using double quotes
"Hello"
Example: Strings in R
message1 <- 'Hola Amigos' print(message1)
message2 <- "Welcome to Programiz" print(message2)
Output
[1] "Hola Amigos“
39
[1] "Welcome to Programiz"
Example

a <- 'Start and end with single quote'


print(a)
b <- "Start and end with double quotes"
print(b)
c <- "single quote ' in between double quotes"
print(c)
d <- 'Double quotes " in between single quote'
print(d)

Output
[1] ‘Start and end with single quote’
[1] "Start and end with double quotes“
[1] "single quote ' in between double quote"
40
[1] ‘Double quote " in between single quote"
Join Strings Together
In R, we can use the paste() function to join two or more strings together.
For example
message1 <- "Programiz" message2 <- "Pro" # use paste() to join two strings
paste(message1, message2)
Output
[1] Programiz Pro
Example
message1 <- "Hello, World!"
message2 <- "Hola, Mundo!“
message3 <- "Hello, World!“
# compare message1 and message2 print(message1 == message2)
# compare message1 and message3 print(message1 == message3)
Output
[1] FALSE
[1] TRUE 41
R Looping
● Loops are used to repeat the process until the expression (condition) is TRUE. R
uses three keywords for, while and repeat for looping purpose. Next and break,
provide additional control over the loop.
● The break statement exits the control from the innermost loop. The next statement
immediately transfers control to return to the start of the loop and statement after
next is skipped.
● The value returned by a loop statement is always NULL and is returned invisibly.
There are three types of loops in R programming:
•For Loop
•While Loop
•Repeat Loop 42
For Loop in R

● It is a type of control statement that enables one to easily construct an R


loop that has to run statements or a set of statements multiple times.
● It is an entry-controlled loop, in this loop, the test condition is tested first,
then the body of the loop is executed, the loop body would not be executed
if the test condition is false.
Syntax:

for (initialization_Statement; test_Expression; update_Statement)


{
// statements inside the body of the loop
}
43
Flow Diagram:

44
Example 1: Program to display numbers from 1 to 5 using for loop in R

fruits<-list("apple","banana","cherry")

for(x in fruits)
{
print(x)
}

Output

[1] “apple”
[1] “banana”
[1] “cherry” anana"
[1] "cherry«[1]{ 45
While Loop in R

● It is a type of control statement that will run a statement or a set of statements repeatedly
unless the given condition becomes false.
● It is also an entry-controlled loop, in this loop, the test condition is tested first, then the
body of the loop is executed, the loop body would not be executed if the test condition is
false.
Syntax:
while (expression)
{
statement
} 46
Flow Diagram:

47
Example: Print i as long as i is less than 6

i <- 1
while (i < 6) {
print(i)
i <- i + 1
}

Output
[ 1[[
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5 48
[1] 4
Repeat loop
● A repeat loop is used to iterate a block of code.
● It is a special type of loop in which there is no condition to exit from the loop.
● For exiting, we include a break statement with a user-defined condition.
● This property of the loop makes it different from the other loops.
● A repeat loop constructs with the help of the repeat keyword in R.
● It is very easy to construct an infinite loop in R.
Syntax:
repeat {
commands
if(condition) {
break } }
49
Flow Diagram:

50
Example

v <- c("Hello","repeat","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5)
{
break
}
}

51
Output:

[1] “Hello” “repeat” “loop”


[1] “Hello” “repeat” “loop”
[1] “Hello” “repeat” “loop”
[1] “Hello” “repeat” “loop”

52
Data Import and Export
Data Import
Reading a Comma-Separated Value(CSV) File

Method 1: Using [Link]() Function Read CSV Files into R

The function has two parameters:

● [Link](): It opens a menu to choose a CSV file from the desktop.


● header: It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the
variable name is present else put F/False.

data1 <- [Link]([Link](), header=T)

data1
Data Import
Method 2: Using [Link]() Function

This function specifies how the dataset is separated, in this case we take sep="/t " as an argument.

data2 <- [Link]([Link](), header=T, sep="/t ")

data2
Data Import
Reading a Tab-Delimited(txt) File
Tab-delimited files are commonly used for data exchange and R supports reading them with built-in methods.

Method 1: Using [Link]() Function


The function has two parameters:

● [Link](): It opens a menu to choose a txt file from the desktop.


● header: It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the
variable name is present else put F/False

data3 <- [Link]([Link](), header=T)

data3
Method 2: Using [Link]() Function

This function specifies how the dataset is separated, in this case we take sep="\t" as the argument.

data4 <- [Link]([Link](), header=T, sep="\t")

data4

57
Data Import

[Link]() reads in CSV files where values are comma separated


read.csv2() reads in CSV files where values are semicolon separated
The [Link] function is used when numbers in your file use periods as decimals.
The read. delim2 function is used when numbers in your file use commas as decimals.
The read. table() function in R can be used to read a text file's contents.
Exporting data to a text file
[Link]():
The R base function [Link]() can be used to export a data frame or a matrix to a text file.
Exporting data to a text file
Exporting data to a text file
write_tsv():
This write_tsv() method is also used for to export data to a tab separated (“\t”) values by using
the help of readr package.
Exporting data to a csv file
[Link]():
The R base function [Link]() can also be used to export a data frame or a matrix to a csv
file.
Exporting data to a csv file
Exporting data to a csv file
[Link]():
This [Link]() method is recommendable for exporting data to a csv file. It uses “.” for the
decimal point and a comma (“, ”) for the separator.
Descriptive Statistics in R
• In Descriptive statistics in R Programming Language, we describe our data with the help of
various representative methods using charts, graphs, tables, excel files, etc.
• Most of the time it is performed on small data sets and this analysis helps us a lot to predict
some future trends based on the current findings.
• Some measures that are used to describe a data set are measures of central tendency and
measures of variability or dispersion.

65
Process of Descriptive Statistics in R
● The measure of central tendency
● Measure of variability

66
Measure of central tendency
It represents the whole set of data by a single value. It gives us the location of central points. There are three
main measures of central tendency:

● Mean
● Mode
● Median

67
Measure of variability
In Descriptive statistics in R measure of variability is known as the spread of data or how well is our data is
distributed. The most common variability measures are:

● Range
● Variance
● Standard deviation

68
Descriptive Analysis in R
• Descriptive analyses consist of describing simply the data using some summary statistics
and graphics

Import your data into R:


# R program to illustrate

# Descriptive Analysis

# Import the data using [Link]()

myData = [Link]("[Link]",

stringsAsFactors = F)

# Print the first 6 rows


print(head(myData))

69
70
Data visualization with R and ggplot2
• The ggplot2 ( Grammar of Graphics ) is a free, open-source visualization package widely used in R
Programming Language.
• It includes several layers on which it is governed. The layers are as follows:
• Data: The element is the data set itself.
• Aesthetics: The data is to map onto the Aesthetics attributes such as x-axis, y-axis, color, fill, size,
labels, alpha, shape, line width, line type.
• Geometrics: How our data being displayed using point, line, histogram, bar, boxplot.
• Facets: It displays the subset of the data using Columns and rows.
• Statistics: Binning, smoothing, descriptive, intermediate.
• Coordinates: the space between data and display using Cartesian, fixed, polar, limits.
• Themes: Non-data link.

71
72
mtcars(motor trend car road test) dataset

mtcars dataset which includes 32 car brands and 11 attributes

73
1. Data Layer

The data layer we define the source of the information to be visualize, let’s use the mtcars dataset in the ggplot2
package.

library(ggplot2)
library(dplyr)

ggplot(data = mtcars) + labs(title = "MTCars Data Plot")

74
2. Aesthetic Layer

Here we will display and map dataset into certain aesthetics.

ggplot(data = mtcars, aes(x = hp, y = mpg, col =


disp))+labs(title = "MTCars Data Plot")

75
3. Geometric layer

The geometric layer control the essential elements, see how our data being displayed using point, line, histogram, bar, boxplot.

ggplot(data = mtcars, aes(x = hp, y = mpg, col =


disp)) +
geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")

76
# Adding size
ggplot(data = mtcars, aes(x = hp, y = mpg, size = disp)) +
geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")

# Adding shape and color


ggplot(data = mtcars, aes(x = hp, y = mpg, col = factor(cyl),
shape = factor(am))) +geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")

# Histogram plot
ggplot(data = mtcars, aes(x = hp)) +
geom_histogram(binwidth = 5) +
labs(title = "Histogram of Horsepower",
x = "Horsepower",
y = "Count")

77
78
4. Facet Layer

● The facet layer is used to split the data up into subsets of the entire dataset and it
allows the subsets to be visualized on the same plot.

p <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) +


geom_point()

p + facet_grid(am ~ .) +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")

p <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) +


geom_point()

p + facet_grid(. ~ cyl) +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")
79
5. Statistics layer

This layer transforms our data using binning, smoothing, descriptive, intermediate summaries.

ggplot(data = mtcars, aes(x = hp, y = mpg)) +


geom_point() +
stat_smooth(method = lm, col = "red") +
labs(title = "Miles per Gallon vs Horsepower")

80
6. Coordinates layer

In these layers, data coordinates are mapped together to the mentioned plane of the graphic and we adjust the axis and
changes the spacing of displayed data with Control plot dimensions.

ggplot(data = mtcars, aes(x = wt, y = mpg)) +


geom_point() +
stat_smooth(method = lm, col = "red") +
scale_y_continuous("Miles per Gallon", limits = c(2, 35),
expand = c(0, 0)) +
scale_x_continuous("Weight", limits = c(0, 25), expand =
c(0, 0)) +
coord_equal() +
labs(title = "Miles per Gallon vs Weight",
x = "Weight",
y = "Miles per Gallon")

81
7. Theme Layer

This layer controls the finer points of display like the font size and background color properties.

ggplot(data = mtcars, aes(x = hp, y = mpg)) +


geom_point() +
facet_grid(. ~ cyl) +
theme([Link] = element_rect(fill = "blue",
colour = "gray")) +
labs(title = "Miles per Gallon vs Horsepower")

82
Histogram of Age Distribution

library(ggplot2)
ggplot(myData, aes(x = Age)) +
geom_histogram(binwidth = 2, fill = "blue", color = "red", alpha = 0.8) +
labs(title = "Age Distribution", x = "Age", y = "Frequency")

● The ggplot2 library to create a histogram


of the 'Age' variable from the 'myData'
dataset.
● The histogram bins have a width of 2,
and the bars are filled with a teal color
with a light gray border.

The resulting visualization shows the distribution of


ages in the dataset.
83
Boxplot of Miles by Gender

ggplot(myData, aes(x = Gender, y = Miles, fill = Gender)) +


geom_boxplot() +
labs(title = "Miles Distribution by Gender", x = "Gender", y =
"Miles") +
theme_minimal()

● Each boxplot represents the


interquartile range (IQR) of Miles for
each gender.
● The plot is titled "Miles Distribution by
Gender," with 'Gender' on the x-axis
and 'Miles' on the y-axis.

We create a boxplot visualizing the distribution of 'Miles' run, segmented by 84


'Gender' from the 'myData' dataset.
Bar Chart of Education Levels

ggplot(myData, aes(x = factor(Education), fill =


factor(Education))) +
geom_bar() +
labs(title = "Education Distribution", x = "Education Level",
y = "Count") +
theme_minimal()

● Each bar represents the count of


observations for each education level.
● The chart is titled "Education
Distribution," with 'Education Level' on
the x-axis and 'Count' on the y-axis.

bar chart illustrating the distribution of 'Education' levels from the 'myData' 85
dataset
Mean

It is the sum of observations divided by the total number of observations. It is also defined as average
which is the sum divided by count.
# R program to illustrate
# Descriptive Analysis

# Import the data using [Link]()


myData = [Link]("[Link]",
stringsAsFactors = F)

# Compute the mean value


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

86
Median

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.

# R program to illustrate
# Descriptive Analysis

# Import the data using [Link]()


myData = [Link]("[Link]", stringsAsFactors = F)

# Compute the median value


median = median(myData$Age)
print(median)

87
Mode

It is the value that has the highest frequency in the given data set. The data set may have no mode if the frequency of all data
points is the same. Also, we can have more than one mode if we encounter two or more data points having the same frequency.

# R program to illustrate
# Descriptive Analysis

# Import the library


library(modeest)

# Import the data using [Link]()


myData = [Link]("[Link]",
stringsAsFactors = F)

# Compute the mode value


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

88
Range

The range describes the difference between the largest and smallest data point in our data set. The bigger the range, the more is the
spread of data and vice versa.
# R program to illustrate
# Descriptive Analysis

# Import the data using [Link]()


myData = [Link]("[Link]",
stringsAsFactors = F)
# Calculate the maximum
max = max(myData$Age)
# Calculate the minimum
min = min(myData$Age)
# Calculate the range
range = max - min
cat("Range is:\n")
print(range)
# Alternate method to get min and max
r = range(myData$Age)
print(r)
89
Variance

It is defined as an average squared deviation from the mean. It is being calculated by finding the difference
between every data point and the average which is also known as the mean, squaring them, adding all of them,
and then dividing by the number of data points present in our data set.

# R program to illustrate
# Descriptive Analysis

# Import the data using [Link]()


myData = [Link]("[Link]",
stringsAsFactors = F)

# Calculating variance
variance = var(myData$Age)
print(variance)

90
Standard Deviation

It is defined as the square root of the variance. It is being calculated by finding the Mean, then subtract each number from the
Mean which is also known as average and square the result. Adding all the values and then divide by the no of terms followed the
square root.

# R program to illustrate
# Descriptive Analysis

# Import the data using [Link]()


myData = [Link]("[Link]",
stringsAsFactors = F)

# Calculating Standard deviation


std = sd(myData$Age)
print(std)

91
Exploratory Data Analysis in R Programming

● Exploratory Data Analysis (EDA) is a process for analyzing and summarizing the key characteristics of a dataset,
often using visual methods.
● It helps to understand the structure, relationships and potential issues in data before conducting formal modeling.

Key Aspects of EDA

● Characteristics of the data: Understanding the variables and their distribution.


● Relationships between variables: Investigating how variables correlate with each other.
● Identifying key variables: Finding the most important variables that can be used in the analysis.

EDA is an iterative process that involves:

1. Generating questions about the data.


2. Searching for answers using visualization, transformation and modeling.
3. Refining the questions or generating new ones based on what has been learned.
92
In R, we perform EDA through two primary approaches:

1. Descriptive Statistics: Summarizing data using numerical methods such as mean, median and standard deviation.
2. Graphical Methods: Visualizing data through plots like histograms, box plots and scatter plots.

In this example, we will use the built-in iris dataset in R to show EDA techniques.

data("iris")
head(iris)

93
1. Descriptive Statistics for EDA

● Descriptive statistics involve summarizing and describing the main features of a dataset through
numerical measures like mean, median, mode, standard deviation, variance and range.
● These statistics help in understanding the central tendency, dispersion and overall distribution of
the data

94
1.1 Measures of Central Tendency

● mean(): Calculates the average of a numeric variable.


● median(): Finds the middle value of a numeric variable.
● getmode(): Custom function to find the most frequent value (mode) of a variable.

getmode <- function(v) {


uniqv <- unique(v)
uniqv[[Link](tabulate(match(v, uniqv)))]
}

cat("\n Mean Sepal Length: ",mean(iris$[Link]))


cat("\n Median Sepal Length: ",median(iris$[Link]))
cat("\n Mode Sepal Length: ",getmode(iris$[Link]))

95
1.2 Measures of Dispersion

● var(): Computes the variance, indicating data spread.


● sd(): Computes the standard deviation, showing variation extent.
● range(): Finds the minimum and maximum values.
● IQR(): Calculates the interquartile range (spread between 25th and 75th percentiles).

cat("\n Variance: ", var(iris$[Link]))


cat("\n Standard Deviation: ", sd(iris$[Link]))
cat("\n Range: ", range(iris$[Link]))
cat("\n Interquartile Range (IQR): ", IQR(iris$[Link]))

96
1.3 Correlation

Next, we examine the relationships between numerical variables by computing the correlation matrix.

● corr(): Computes the correlation matrix for a set of numeric variables, showing relationships between
them.

cor(iris[, 1:4])

97
2. Graphical Methods for EDA

● Graphical methods involve visualizing the data using plots such as histograms, box plots, scatter plots and bar charts.

● These visualizations help in identifying patterns, trends, outliers and the distribution of data, making it easier to
interpret and communicate insights.

We begin by plotting histograms to visualize the distribution of variables like Sepal Length.

● ggplot(): Initializes a plot object in ggplot2, taking data and aesthetic mappings.
● geom_histogram(): Creates a histogram to visualize the distribution of a variable.

[Link]("ggplot2")
library(ggplot2)

ggplot(iris, aes(x = [Link])) +


geom_histogram(binwidth = 0.2, fill = "blue", color = "white", alpha = 0.7) +
labs(title = "Histogram of Sepal Length", x = "Sepal Length", y = "Frequency") +
theme_minimal()

98
Next, we can plot the density curve for Sepal Length:

● geom_density(): Plots a smooth curve (kernel density estimate) to show the distribution of a continuous variable.

ggplot(iris, aes(x = [Link])) +


geom_density(fill = "blue", alpha = 0.7) +
labs(title = "Density Curve for Sepal Length", x = "Sepal Length", y = "Density") +
theme_minimal()

99
2.2 Box Plot

A box plot is useful to visualize the spread and potential outliers in the data.

● geom_boxplot(): Generates a boxplot to show the distribution and detect outliers.

ggplot(iris, aes(x = Species, y = [Link], fill = Species)) +


geom_boxplot() +
labs(title = "Box Plot of Sepal Length by Species", x = "Species", y = "Sepal Length") +
theme_minimal()

100
2.3 Scatter Plot

We can also examine the relationships between two numerical variables with scatter plots. For example, we’ll plot Sepal
Length against Sepal Width.

● geom_point(): Creates a scatter plot to show the relationship between two variables.

ggplot(iris, aes(x = [Link], y = [Link], color = Species)) +


geom_point() +
labs(title = "Scatter Plot of Sepal Length vs Sepal Width", x = "Sepal Length", y = "Sepal Width")
+
theme_minimal()

101
2.4 Pairwise Plot

For more comprehensive visualization, a pairwise scatter plot (or pairs plot) can help us see all pairwise relationships
between the numerical variables in the dataset.

● pairs(): Creates a scatterplot matrix to visualize pairwise relationships between


multiple variables
● pch: Symbol used in the scatterplots (e.g., 1 for circles)
● col: Color of points in the scatterplots

pairs(iris[, 1:4], col = iris$Species, pch = 21)

102
Dirty Data
• Dirty data refers to data that is inaccurate, incomplete, inconsistent, or contains
errors, making it challenging to use for analysis or decision-making.
• Dirty data can be a result of various factors, including human error, data entry
mistakes, software bugs, hardware malfunctions, or problems during data
migration and integration

103
There are Several Types of Dirty Data:
1. Missing values: Some records may lack certain attributes, leaving gaps in the dataset.
2. Inconsistent data: Inconsistencies occur when the same data element is recorded differently across
various parts of the dataset.
3. Duplicate data: The same information is recorded multiple times, leading to redundant entries.
4. Outliers: These extreme values deviate significantly from the rest of the data, potentially skewing
analysis results.
5. Incorrect data: Data may be wrongly entered, outdated, or poorly validated.
6. Non-standardised data: Differences in formatting and units of measurement can lead to confusion
and errors during analysis.

104
What Causes Dirty Data?
Dirty data can be caused by various factors, both human and technological. Some of the common reasons for dirty data
include:

1. Data Entry Errors: Human errors during manual data entry can lead to typos, misspellings, and incorrect data being
recorded.

2. Software Bugs: Errors in data collection and storage software can introduce inaccuracies into the dataset.

3. Lack of Validation: When data is not properly validated during data entry, it increases the likelihood of incorrect or
invalid information being included.

4. Data Integration and Migration: During the process of integrating data from multiple sources or migrating data
between systems, errors can occur, leading to inconsistencies and data quality issues.

5. Inadequate Data Cleaning: If data cleaning and preprocessing steps are not performed properly or skipped
altogether, dirty data can persist in the dataset.

105
6. Outdated Information: Data can become dirty if it's not regularly updated and becomes obsolete over time.

7. Duplicate Records: Poor data management practices or system errors can result in the creation of duplicate records.

8. Non-Standardised Data: Inconsistent formatting, units of measurement, or naming conventions can introduce confusion
and errors in the dataset.

9. External Factors: Sometimes, external events or circumstances can influence data quality, such as natural disasters, power
outages, or cyber-attacks.

10. Data Privacy Issues: When individuals provide inaccurate information or intentionally misrepresent themselves to
protect their privacy, it can lead to dirty data.

11. Sensor or Instrument Malfunctions: In IoT (Internet of Things) applications, malfunctioning sensors or instruments can
generate inaccurate data.

106
Visualizing a single variable
Visualizing a single variable in R depends on whether the data is categorical (discrete groups) or
numerical (continuous values).

107
108
Multivariate-data-visualization

• A method for visualizing data with numerous variables is called multivariate

data visualization with R.

• In this method, graphs and charts are made to show how the various factors

relate to one another.

• The programming language R, which is frequently used for data

visualization, provides a number of tools for the visualization of multivariate

data.

109
● Scatterplots: In multivariate data visualization, scatterplots can be used to visualize the
relationship between multiple variables by coloring the points or adding shapes or sizes.
● Line Graphs: In multivariate data visualization, Line graphs can be used to compare the trends
of two variables or to show the trends of several variables across time.
● Bar Charts: In multivariate data visualization, By stacking or grouping the bars, bar charts can
be used to compare the values of several category variables.
● Heatmaps: Heatmaps are a type of plot used to display the relationship between multiple
variables. Heatmaps use colors to represent the values of the variables, and rows and columns
represent the different variables.
● Boxplots: In multivariate data visualization, the distributions of a continuous variable over
various categories can be compared using boxplots.
● Correlation Matrix: In multivariate data visualization, to see the connections between
variables, a heatmap or other graphical display can be used to visualize a correlation matrix.

110
Multivariate-data-visualization
• Scatterplot Matrix

• Correlation Matrix Heatmap

111
Multivariate-data-visualization
• Parallel Coordinate Plot

• Bubble Chart

112

You might also like