Unit 1 significantly reduces the time required
to run a R script.
Introduction of the Language
4. Efficient in Software Development
History
• It may be used for both statistical
• R is a high-level programming
computing and software development.
language and software environment for
statistical • Supports both procedural
programming and object-oriented
analysis, graphics representation and
programming.
reporting.
• It is widely used as a statistical
• It is an implementation of the S
software and data analysis tool.
programming language.
5. Extensive Range of Packages
• R was developed by Ross Ihaka and
Robert Gentleman at the university of • R contains a large set of packages,
which can be used in any branch like
Auckland, New Zealandin 1991.
astronomy, biology, statistic etc.
• The language name “R” is inspired
• Packages in R Programming language
after the first character of its author's
are a set of R functions, compiled code,
name.
and sample data.
Features of R
• R has CRAN (Comprehensive R
1. Open Source Archive Network), which is a repository
holding more than 10, 0000
• It is no-cost (free) software.
packages.
• Without a license it can be installed
and used in any organization. 6. Enables Quick Calculations
• Anyone can contribute towards the • It includes variety of operators to
further development of R, customize its perform varied calculations.
packages, and add more features.
• R supports a wide range of
2. Compatibility with Multiple Platforms complicated operations on vectors,
arrays, data frames, and other data
It is platform-independent
objects
programming language available on
widely used platforms like Windows, of various sizes.
Linux, and
7. Integration with Other Technologies
Mac.
Allows integrating with programs
3. No Compilation written in other languages like C, C++,
python etc.
• It is an interpreted language;
therefore, the compiler is not required Advantages of R
to compile the code into an executable
Refer “Features of R”
program.
Disadvantages of R
• The interpreter in R evaluates the
1. Complicated language
source code step by step and converts
it into machine code. Thus,
It is best suited for people with previous • R does not require datatype to be
programming experience. declared to a variable.
2. Not secure. • A variable can take any datatype in R.
R doesn‟t have basic security measures. • It can hold any R object such as a
function, the result of an analysis or a
It is not a good choice for making web-
plot.
safe applications. R cannot be
embedded in web browsers. Variable Assignment
3. Slow. Assignment operators are used to
assign values to variables.
R is slower than other programming
languages like Python or MATLAB. In R Variable assignment is done in
three ways:
4. Memory management is not efficient
[Link] leftward operator
Consume lot of memory space. Stores
data in physical memory and thus is not variable_name <- value
beneficial for Bigdata.
Example:
5. It lacks uniform
>A<-10
documentation/package quality.
>A
Documentation and packages can be
uneven, inconsistent, or even missing. [1] 10
Application of R 2. Using equal to operators (=).
• In Data Science for statistical variable_name = value
computing and design.
Example:
• Quantitative analysts use it as
>A=10
programming tool for data importing
and cleaning. >A
• Used as a fundamental tool in finance. [1] 10
• In environmental science, R is used to 3. Using rightward operator
analyse and simulate environmental
value -> variable_name
data,
Example:
climate data, and ecological data.
10->A
Variable
>A
• Variable is the name given to the
allocated memory which can store the [1] 10
value that is
Removing Variables
subjected to change within the scope of
• rm() function is used to remove
the program.
variables.
• The name given to a variable is known
• This frees the memory so that R can
as variable name.
store more objects, although it does not
necessarily free up memory for the { variable_numeric<- 3532
operating system.
cat(variable_numeric,"\n")
• There is no “undo”; once the variable
cat("The data type of variable_numeric
is removed.
is ",class(variable_numeric),"\n\n")
Example
}
x <- 2*pi
Output :
>x
3532
[1] 6.283185
The data type of variable_numeric is
> rm(x) # x variable is removed numeric
>x 3. Integer Data type
Error: object 'x' not found {
> rm(x,a,y) # removing multiple variable_integer<- 133L
variables
cat(variable_integer,"\n")
Reserved Variable/Object Names
cat("The data type of variable_integer is
Data Types ",class(variable_integer),"\n\n")
• Data types specifies the kind of data }
that can be stored in a variable.
Output :
• For effective memory consumption
133
and precise computation, the right data
type must be The data type of variable_integer is
integer
selected.
4. Complex Data type
• Basic data types in R:
{ variable_complex<- 3+2i
Example
cat(variable_complex,"\n")
1. Logical Data type
cat("The data type of variable_complex
{
is ",class(variable_complex),"\n\n")
variable_logical<- TRUE
}
cat(variable_logical,"\n")
Output :
cat("The data type of variable_logical is
",class(variable_logical),"\n\n") 3+2i
} The data type of variable_complex is
complex
Output :
6. Character Data type {
TRUE
variable_char<- "Learning r
The data type of variable_logical is
programming" cat(variable_char,"\n")
logical
cat("The data type of variable_char is
2. Numeric Data type
",class(variable_char),"\n\n")
} 1. Vector
Output : 2. List
Learning r programming The data type 3. Matrix
of variable_char is character
4. Array
7. Raw Data type {
5. Data Frame
variable_raw<- charToRaw("Learning r
6. Factors
programming")
Vector
cat(variable_raw,"\n")
• A vector is a basic data structure that
cat("The data type of variable_char is
can hold a sequence of elements (data
",class(variable_raw),"\n\n") }
values) of the
Output :
same data type.
4c 65 61 72 6e 69 6e 67 20 72 20 70
• It is one-dimensional and can hold
72 6f 67 72 61 6d 6d 69 6e 67
numeric data, character data, or logical
The data type of variable_char is raw data.
Class() Function • The elements in the vector are known
as components.
This built-in function is used to
determine the data type of the variable • Every element in a vector is accessed
provided to it. by the index number.
The R variable to be checked is passed • In R the vector index starts with 1.
to this as an argument and it prints the
Example of vector in R:
data type in return.
Vector is classified into two categories
Syntax:
1. Atomic vectors :
class(variable)
In an atomic vector, all the elements
Example:
are of the same type. or An atomic
var1 = "hello" vector is a
print(class(var1)) homogeneous set of elements.
Output: "character" 2. List :
Data Structures in R: The list is a heterogeneous set of
elements.
In R programming, a data structure
refers to a particular way of organizing
and storing data so that it can be
accessed and used efficiently. R
provides
Accessing element from vector using
several built-in data structures to
index
handle different types of data and
Example:
operations.
a<- c(2,3,4,5,6,7) 3. Sorting() function : use sort values.
print(a[2]) Example:
•Output : Using sort() function
[1] 3 a<-c(30,40,10,20,50)
1. Length() Function : use to find sort(a)
length of a vector. Output:
Example : [1] 10 20 30 40 50
a<- c(2,3,4,5,6,7) By Descending order:
length(a) Example:
•Output : a<-c(10,20,30,40,50)
[1] 6 b<sort(a , decreasing=TRUE)
2. rep() Function : use to find print(b)
repeated values. Output:
rep(X, times, each, [Link]) [1] 50 40 30 20 10
Example : 4. Update() function :
a<-rep(c(1,2,3), times=2) Example:
print(a) a<-c(10,20,30,40,50)
a<-rep(c(1,2,3), each=2) a[3]<-80
print(a) print(a)
a<-rep(1:3, [Link]=7) Output:
print(a) [1] 10 20 80 40 50
Output: 5. Delete() Function:
a<-rep(c(1,2,3), times=2) Example : using rm() function or using
negative
> print(a)
index
[1] 1 2 3 1 2 3
a<-c(10,20,30,40,50)
> a<-rep(c(1,2,3), each=2)
print(a)
> print(a)
rm(a)
[1] 1 1 2 2 3 3
print(a)
> a<-rep(1:3, [Link]=7)
a<-c(10,20,30,40,50)
> print(a)
a<-a[-3]
[1] 1 2 3 1 2 3 1
print(a)
a
Operations of vectors >
1. Combining vector: > b<-a-5
Example : > print(b)
a<-c(10,20,30,40,50) [1] 5 15 25 35 45
b<-c("hi","hello") >
h<-c(a,b) > b<-a/5
print(h) > print(b)
Output: [1] 2 4 6 8 10
[1] "10" "20" "30" "40" >
"50" "hi" "hello" > b<-a*5
2. Arithmetic operators: > print(b)
1. Addition [1] 50 100 150 200 250
2. Substraction >
3. Multiply > b<-a^2
4. Division > print(b)
5. Square [1] 100 400 900 1600 2500
Example 2. List
a<-c(10,20,30,40,50) • List is a collection of heterogeneous
element or data types.
b<-a+5
• List is one dimensional method.
print(b)
• List() function can be used to create
b<-a-5
list in R.
print(b)
Syntax,
b<-a/5
Variable_name <-list(value1, value2,
print(b) …..)
b<-a*5 Creating list in R using list() function
print(b) Example
b<-a^2 a<-list("hi", "Hello")
print(b) print(a)
Output: a<-list("ram", 25, c(1,2,3))
b<-a+5 print(a)
> print(b) Output
[1] 15 25 35 45 55 •[[1]]
•[1] "hi" a<-list("hi", "Hello","java")
•[[2]] c("hi","java")%in%a
•[1] "Hello" Output
> a<-list("ram", 25, c(1,2,3)) [1] TRUE TRUE
> print(a) Naming Elements While Creating the
List
[[1]]
In R, you can name the elements of a
[1] "ram"
list either when you create it or after it’s
[[2]] created.
[1] 25 Example:
[[3]] my_list <- list(name = “Ram", age =
25, scores = c(80, 90, 95))
[1] 1 2 3
print(my_list)
Accessing element using index
Output:
Example ;
$name
a<-list("hi", "Hello","java")
[1] “Ram"
a[2]
$age
Output :
[1] 25
[[1]]
$scores
[1] "Hello"
[1] 80 90 95
1. List length() :
Naming Elements After Creating the List
Example :
Example:
a<-list("hi","Hello","java")
my_list <- list(“Ram", 25, c(80,
length(a)
90, 95))
Output :
# Add names after list creation
[[1]]
names(my_list) <- c("name",
[1] 3
"age", "scores")
2. Check if item is exist in list:
print(my_list)
Example :
Output :
a<-list("hi", "Hello","java")
$name
"hi"%in%a
[1] “Ram"
Output
$age
[1] TRUE
[1] 25
Multiple element :
$scores [1] 25
[1] 80 90 95 $city
Accessing Elements by Name [1] "Bangalore"
my_list$name $degree
my_list$age [1] "MSc"
[1] “Ram" $year
[1] 25 [1] 2025
Change Elements in a List Combine Two Lists Using c()
# Change age Append to a List
my_list$age <- 26 Example:
# Change city using index my_list <- list(name = “Ram", age =
25)
my_list[["city"]] <- "Mumbai“
my_list <- append(my_list, list(city =
print(my_list)
"Bangalore"))
# Step 1: Create initial list
print(my_list)
my_list <- list(
Output :
name = “Ram",
$name
age = 25,
[1] “Ram"
city = "Bangalore"
$age
)
[1] 25
# Step 2: Create another list
$city
new_data <- list(
[1] "Bangalore"
degree = "MSc",
Append at Specific Position
year = 2025
my_list <- list(name = “Ram", age =
) 25)
# Step 3: Combine the two lists using my_list <- append(my_list, list(city =
c() "Bangalore"), after = 1)
my <- c(my_list, new_data) # Insert after 1st element
# Step 4: Print the combined list print(my_list)
print(my) Output :
$name $name
[1] “Ram" [1] “Ram"
$age $city
[1] "Bangalore" output
$age [,1] [,2] [,3]
[1] 25 [1,] 1 2 3
3. Matrix [2,] 4 5 6
• A matrix in R is a two-dimensional Accessing Matrix Elements
data structure (like a table) where
m[1, 2] # Element in 1st row, 2nd
all elements are of the same data type column
(usually numeric).
m[2, ] # Entire 2nd row
• It’s an important structure in data
m[, 3] # Entire 3rd column
analysis, statistics, and machine
Add Row and Column to a Matrix in R
learning.
• # Create original 3x3 matrix
• In R to create matrix we are matrix()
function. mat <- matrix(c(1, 2, 3,
Creating a Matrix in R 4, 5, 6,
# Create a 2x3 matrix (2 rows, 3 7, 8, 9),
columns) nrow = 3, byrow = TRUE)
Example # Add a new row (same number of
columns)
m <- matrix(1:6, nrow = 2, ncol =
new_row <- c(10, 11, 12)
3)
mat <- rbind(mat, new_row)
print(m)
# Add a new column (same number of
Output
rows)
[,1] [,2] [,3]
new_col <- c(100, 200, 300, 400)
[1,] 1 3 5
mat <- cbind(mat, new_col)
[2,] 2 4 6
# Print final matrix
Arguments of matrix()
print(mat)
•data: the input vector
[,1] [,2] [,3] [,4]
•nrow: number of rows
[1,] 1 2 3 100
•ncol: number of columns
[2,] 4 5 6 200
•byrow: if TRUE, fills matrix by rows
[3,] 7 8 9 300
Example
[4,] 10 11 12 400
m <- matrix(1:6, nrow = 2, byrow =
•Note:
TRUE)
• rbind() → Adds a row
print(m)
• cbind() → Adds a column
Function print(solve(mat)) # Only
works if matrix is square and non-
singular
Description
Matrix Operations in R: Addition,
dim(mat)
Returns dimensions (rows, cols) Subtraction, Multiplication, Division
t(mat)
Transposes the matrix
4. Array
diag(mat)
In R programming, an array is a multi-
Extracts diagonal elements
dimensional data structure that can
diag(n) store
Creates an identity matrix of size n x n
data in more than two dimensions (like
solve(mat) 2D, 3D, etc.). All elements in an array
Computes the inverse of a square must
matrix
be of the same data type (either all
# Create a 3x3 matrix numeric, character, or logical).
mat <- matrix(c(1, 2, 3, •Key Points of
4, 5, 6, Arrays in R
7, 8, 10), Arrays are homogeneous (same data
type).
nrow = 3, byrow = TRUE)
Can be multi-dimensional (2D, 3D,
# 1. Get dimensions
etc.).
print("Dimensions:")
Created using the array() function.
print(dim(mat)) # Returns c(3, 3)
Syntax,
# 2. Transpose
array(data,
print("Transpose:") dim)
print(t(mat)) data: vector of elements.
# 3. Diagonal elements dim: numeric vector giving the
dimensions of the array.
print("Diagonal Elements:")
2. Accessing Elements
print(diag(mat)) # Returns c(1, 5, 10)
Example
# 4. Create identity matrix of size 3x3
# Access element at 1st row, 2nd
print("Identity Matrix:")
column, 1st matrix
print(diag(3))
arr[1, 2, 1]
# 5. Inverse of matrix
# Output: 3
print("Inverse of Matrix:")
1. Creating an Array
Example :
# Create a 2x3x2 array addition, subtraction, etc.
arr <- array(1:12, dim = c(2, 3, 2)) ,,1
print(arr)
[,1] [,2] [,3]
Output :
[1,] 1 3 5
,,1
[2,] 2 4 6
[,1] [,2] [,3]
,,2
[1,] 1 3 5
[,1] [,2] [,3]
[2,] 2 4 6
[1,] 7 9 11
,,2
[2,] 8 10 12
[,1] [,2] [,3]
,,1
[1,] 7 9 11
[,1] [,2] [,3]
[2,] 8 10 12
[1,] 13 15 17
,,1
[2,] 14 16 18
[,1] [,2] [,3]
,,2
[1,] 1 3 5
[,1] [,2] [,3]
[2,] 2 4 6
[1,] 19 21 23
,,2
[2,] 20 22 24
[,1] [,2] [,3]
Matrix Multiplication (%*%)
[1,] 7 9 11
This is proper matrix multiplication
[2,] 8 10 99 where the number of columns in the
first matrix must equal the number of
3. Modifying Elements
rows in the second.
# Change the value at position
🔹 Example:
(2, 3, 2)
# 2x3 matrix
arr[2, 3, 2] <- 99
A <- matrix(c(1, 2, 3, 4, 5, 6), nrow =
print(arr)
2)
4. Built-in Functions with Arrays
# 3x2 matrix
Example :
B <- matrix(c(6, 5, 4, 3, 2, 1), nrow =
# Create a 2x3x2 array 3)
arr <- array(1:12, dim = c(2, 3, 2)) # Matrix multiplication
print(arr) C <- A %*% B
5. Array Arithmetic Operations print("Matrix A:")
Arrays support element-wise print(A)
operations like
print("Matrix B:") # Apply sum over rows (MARGIN = 1)
print(B) row_sums <- apply(arr, 1, sum)
print("Matrix Multiplication Result (A %* cat("\nSum over rows (MARGIN=1):\n")
% B):")
print(row_sums)
print(C)
# Apply mean over columns (MARGIN
Matrix A: = 2)
[,1] [,2] [,3] col_means <- apply(arr, 2, mean)
[1,] 1 3 5 cat("\nMean over columns
(MARGIN=2):\n")
[2,] 2 4 6
print(col_means)
Matrix B:
7. Naming Dimensions
[,1] [,2]
In R, we can assign names to
[1,] 6 3
dimensions of a multi-dimensional array
[2,] 5 2 using the
[3,] 4 1 dimnames() function. This makes it
easier to understand the structure and
Result (A %*% B):
refer to elements
by name.
6. Apply Function Over Array
5. Data Frame
In R, you can use the apply() function
• A data frame is a two-dimensional,
to apply a function over margins
heterogeneous data structure in R. It is
(dimensions) of an
like a
array — like rows, columns, or layers.
table (similar to Excel or CSV files),
Syntax of apply(): where:
apply(X, MARGIN, FUN) • Each column can contain different
data types (numeric, character, logical,
X → the array (or matrix)
etc.).
MARGIN → 1 for rows, 2 for columns, 3
• Each row represents a record or
for third dimension (if array
observation.
has 3 dims),
• Each column represents a variable or
FUN → the function to apply (like sum, feature
mean, max, etc.)
🔹
# Create a 3D array (2 rows, 3 columns,
Syntax:
2 layers)
[Link](column1 = data1, column2
arr <- array(1:12, dim = c(2, 3, 2))
= data2, ...)
cat("Original Array:\n")
# Creating a simple student data frame
print(arr)
student_data <- [Link](
Name = c("Amit", "Priya", "Ravi"), In R programming, a factor is a special
data structure used for categorical data
Age = c(20, 21, 19),
— that
Marks = c(85, 90, 78),
is, data that can take a limited number
Passed = c(TRUE, TRUE, FALSE) of unique values (known as levels).
) What is a Factor in R?
print(student_data) A factor stores categorical variables in
the form of integers associated with
Output:
category
Name Age Marks Passed
labels (levels).
1 Amit 20 85 TRUE
It is used to handle both nominal and
2 Priya 21 90 TRUE ordinal categorical data.
3 Ravi 19 78 FALSE Why Use Factors?
Accessing Data Factors reduce memory usage when
storing repeated categorical data.
a. By column:
They enforce valid category values,
student_data$Name # Access the
which helps with data validation.
'Name'
They're essential for modeling in R
student_data$Marks # Access
(e.g., lm(), glm() automatically handle
'Marks'
factors for
b. By index:
categorical variables).
student_data[1, ] # First row
Creating a Factor
student_data[, 2] # Second
# Example: Gender variable
column
gender <- c("male", "female",
student_data[2, 3] # Value at row
"female", "male",
2, column 3
"female")
Modifying Data
gender_factor <- factor(gender)
a. Add new column:
print(gender_factor)
student_data$Grade <- c("B", "A", "C")
class(gender_factor) # "factor"
b. Add new row:
levels(gender_factor) # "female"
new_row <-
"male"
[Link](Name="Sneha", Age=20,
nlevels(gender_factor) #2
Marks=88, Passed=TRUE, Grade="A")
[1] male female female male female
student_data <- rbind(student_data,
new_row) Levels: female male
6. Factors Classes In R Programming
• Classes are the blueprint or prototype object: # Create a list and assign a
which are responsible for providing class
different
person <- list(name = "Alice", age =
properties and different methods to 30)
perform operations on these properties.
class(person) <- "Person"
• It helps to create object and contain
print(person)
its member variables along with
attributes. 2. S4 Class System
• A class can hold both data and S4 is more formal and strict. It requires
functions that is why it is called you to explicitly define classes and
Encapsulated unit. methods.
R has Three class systems :- 🔹 Characteristics:
1. S3 Class Formal class and method definitions.
2. S4 Class Slots (components) and their data
types are specified.
3. Reference Class
Better validation and error-checking.
• class() function is used to check the
class of an object from which the object 🔹 Define an S4
belongs. class:
• [Link]() function returns True if # Define a class
input argument is an object of any of
setClass("Person",
class/model.
slots = list(name = "character",
• [Link]() function returns True if age = "numeric"))
input argument is a function
# Create an object
1. S3 Class System
p <- new("Person", name = "Bob", age
S3 is the simplest and most common = 25)
OOP system in R. It's informal and
# Access slots
flexible
p@name
but lacks strict validation.
:
🔹 Characteristics:
3. Reference Classes (RC)
1. No formal class definitions.
Reference classes (also called R5) are
2. Methods are dispatched based on
mutable and
the object’s class.
behave like objects in languages like
3. Functions are generic and use
Python or Java.
method dispatch (like print, summary,
etc.). 🔹 Characteristics:
🔹 How to define an S3 Fields and methods are part of the
object.
Objects are modified by reference (no Error in num + 4: non-numeric
need to reassign). argument to binary operator
Use setRefClass() to define. [Link]() methods.
Some Commands :- • To solve this error we will convert
non-numeric data into numeric data
•1. Sloop::otype() ---------------> Tells
about type of object but using
firstly we have to install sloop package • Example 1: Perform into vector
by using
• We will convert non-numeric data
•2. [Link](sloop)----------→ from vector into numeric data using
Used to install a Package
[Link]() methods
•3. library(sloop) ----------------→ To load
• num <- "2"
an installed
• res <- [Link](num) + 3
Package.
• print(res)
•[Link](Object_name)--------------→ Type
of a particular Output :
objec 5
library("sloop") #load library Special Values in R
Special values are the terms reserved
list1=list(Name="John",Rollno=5,Std=" to handle the missing data or
10th")
practically infinite number. These
class(list1)="student“ special values can be used to mark
print(list1) missing values in vectors, arrays, or
other data structures.
Print(otype(list1))
Some special values used in R are:
Non-numeric Values In R
1. NA
The “non-numeric argument to binary
operator” error occurs when we 2. NaN
perform arithmetic operations on non- 3. Inf and –Inf
numeric elements.
4. NULL
How to produce this error
1. NA ( Not Available)
• Here we can see, we have to take the
In R, Missing values are often
string element and try to add it
represented by NA
with a numeric element, so it will occur.
Example:
num <- "2"
z = c( 1,2,3, NA,5,NA)
res <- num + 4
# NA is missing Data
print(res)
z [1] 1 2 3 NA 5 NA
Output :
OR R imposes limits on how extreme a
number can be represented.
z = c( 1,2,3)
When a number is too large for R to
Length(z) = 4
represent, the value is deemed to be
Print(z) infinite.
Output: 1 2 3 NA Infinity value is represented by the
special object Inf, which is case
# NA is missing Data
sensitive.
Detect missing values:
Because it represents a numeric value,
[Link]() function is used to detect Inf can be associated only with numeric
missing elements in an object
vectors.
Example:
a <- 90000^100
z = c( 1,2,3, NA,5,NA)
a [1] Inf
# NA is missing Data
R can also represent negative infinity,
z [1] 1 2 3 NA 5 NA with –Inf
Print([Link](z)) Example :
# function to detect a <- -90000^100
Output: a [1] - Inf
NA [1] FALSE FALSE FALSE TRUE FALSE Any nonzero value divided by zero will
TRUE result in infinity (positive or
2. NaN (Not a Number) negative depending on the sign of the
numerator)
NaN is a special case of NA that is
associated only with numeric values. Example
Values that are difficult to quantify are Z = -59/0
labelled as NaN.
[1] –Inf
It is displayed when an arithmetic
Z = 59/0
operation yields a result that is not a
[1] Inf
number.
Print([Link](Z))
Example, dividing zero by zero
produces NaN. # function to detect
Example: 4. NULL
C = 0/0 NULL represents null value.
Print(c) NULL is often used to explicitly define
an “empty” entity, which is quite
Output : NaN
different from a “missing”
3. Infinity (Inf and –Inf)
entity specified with NA.
Example :
c(2,4,NA,8) better understanding of the data and
the potential implications of the
[1] 2 4 NA 8
conversion.
c(2,4,NULL,8)
Example :
[1] 2 4 8
nums_as_chars <- c("1", "2", "3")
Unlike NA, NULL cannot take up a
position in the vector. Thus, dose not nums <- [Link](nums_as_chars)
assigns NULL to multiple
class(nums)
positions in a vector.
[1] "numeric"
Example > c(NA,NA,NA)
Explicit coercion functions:
[1] NA NA NA
• Explicit coercion functions in R allow
c(NULL,NULL,NULL) for controlled conversion
NULL between different data types.
Coercion Functions in R • These functions are typically prefixed
with as. followed by the desired
Coercion is the process of converting
one data type into another. data type.
There are two types of coercion: • Some of the most commonly used
explicit coercion functions are:
1. Implicit Coercion:
1. [Link](): Converts to character
This happens automatically when an
type.
operation demands it.
num <- 123
Example, when combining a numeric
and a character string, char_num <- [Link](num)
R implicitly converts the numeric to a class(char_num)
character.
Output:
Example :
[1] "character"
data <- c(1, 2, "three")
2. [Link](): Converts to a list.
class(data)
Ex:
[1] "character"
vector_data <- c(1, 2, 3)
2. Explicit Coercion:
list_data <- [Link](vector_data)
• Explicit coercion requires the
class(list_data)
programmer to intentionally specify the
Output:
type conversion using specific
functions. [1] "list"
• This method gives the programmer 3. [Link](): Converts to a data
more control but also demands a frame, especially useful when
working with structured datasets.
Ex: This can be particularly useful when
reading numbers stored as text.
list_data <- list(name = "Alice", age =
25) Example:
df <- [Link](list_data) str_num <- "456"
class(df) actual_num <- [Link](str_num)
Output: class(actual_num)
[1] "[Link]" Output:
4. [Link](): Converts to logical type [1] "numeric"
(TRUE or FALSE).
Basic Plotting in R
Ex:
R language is designed for statistical
val <- 1 computing, graphical data
logical_val <- [Link](val) analysis, and scientific research.
class(logical_val) The technique of graphical insights of
data is called data
Output:
visualization.
[1] "logical"
An effective and accurate data
5. [Link](): Converts to factor type,
visualization is an important part of a
useful for categorical variables.
statistical analysis. Graphs are a
Ex:
powerful tool for data visualization
colors <- c("red", "blue", "green")
R offers a rich set of built-in functions
factor_colors <- [Link](colors) and packages for creating
class(factor_colors) various types of graphs such as
histograms scatter plots, bar charts,
Output:
boxplots etc.
[1] "factor"
6. [Link](): Converts to integer
type.
Ex:
float_num <- 7.89 I
nt_num <- [Link](float_num)
class(int_num)
Output:
[1] "integer"
7. [Link](): Converts to numeric
type.
What a Box Plot Shows
|-----|==============|-----|
Min Q1 Median Q3 Max
Example: Basic Box Plot in R
1. Simple Box Plot of a Numeric Vector
# Create some sample data
x <- c(5, 7, 7, 8, 9, 10, 12, 15, 18, 21,
23, 25, 27, 50)
# Draw box plot
boxplot(x,
main = "Box Plot of x",
ylab = "Values",
col = "lightblue")
Example:
# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 5, 7, 11)
# Create scatterplot
plot(x, y,
main = "Basic Scatterplot",
xlab = "X values",
ylab = "Y values",
pch = 16, # Solid circle
col = "blue")