0% found this document useful (0 votes)
8 views

Vectors

Uploaded by

fruito779
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Vectors

Uploaded by

fruito779
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

20CS2058 / BASICS OF DATA

ANALYTICS - R
PROGRAMMING AND TABLEAU

Module 3
Vector
Packages
• library(tidyverse)
Vector
• is simply a list of items that are of the same
type.
• To combine the list of items to a vector, use
the c() function and separate the items by a
comma.
• Example:
fruits <- c("banana", "apple", "orange")
Cntd
• Vector Length
fruits <- c("banana", "apple", "orange")
length(fruits)
OUTPUT
[1] 3
• Sort a Vector
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
sort(fruits)
sort(numbers)
OUTPUT:
[1] "apple" "banana" "lemon" "mango" "orange"
[1] 2 3 5 7 13 20
Types of vector
There are two types of vectors:
• Atomic vectors, of which there are six types:
logical, integer, double, character, complex, and
raw. Integer and double vectors are collectively
known as numeric vectors.
• Lists, which are sometimes called recursive
vectors because lists can contain other lists.
Difference bw atomic vectors and lists
Atomic vectors are homogeneous, while lists
can be heterogeneous.
The hierarchy of R’s vector types
Vector Data Object Operations:
1. Creation of Vectors:
You can create vectors in R using various methods, including:
Using the ‘c()’ function: This function concatenates elements to form a vector.
Using the ‘:’ operator: This operator creates a sequence of values.
Using functions like ‘seq()’, ‘rep()’, or ‘vector()’ to generate specific types of vectors.
2. Element-wise Operations:
Element-wise operations are performed on vectors by applying an operation to each corresponding pair of
elements. Common element-wise operations include:
Addition: ‘vector1 + vector2’
Subtraction: ‘vector1 - vector2’
Multiplication: ‘vector1 * vector2’
Division: ‘vector1 / vector2’
3. Aggregation Functions:
R provides functions to perform aggregations on vectors, such as:
Sum: ‘sum(vector)’
Mean: ‘mean(vector)’
Minimum: ‘min(vector)’
Maximum: ‘max(vector)’
Median: ‘median(vector)’

4. Vector Indexing and Slicing:


You can access specific elements of a vector using indexing. R uses 1-based indexing, where the first
element is accessed with index 1. Slicing allows you to extract a subset of elements from a vector based on
indices or logical conditions.
5. Vector Comparison:
R allows you to compare vectors element-wise, producing logical vectors as output. Common comparison
operators include:
Equal to: ‘vector1 == vector2’
Not equal to: ‘vector1 != vector2’
Greater than: ‘vector1 > vector2’
Less than: ‘vector1 < vector2’

6. Vector Functions and Transformations:


R provides various functions to perform transformations and computations on vectors, including:
Square root: ‘sqrt(vector)’
Exponential: ‘exp(vector)’
Logarithm: ‘log(vector)’
Sorting: ‘sort(vector)’
Examples – Creation of Vectors:
1. Using the ‘c()’ function:
# Create a numeric vector
numeric_vector <- c(1.5, 2.3, 3.7, 4.1)
print(numeric_vector) # Output: 1.5 2.3 3.7 4.1

# Create a character vector


character_vector <- c("apple", "banana", "orange")
print(character_vector) # Output: "apple" "banana" "orange"

# Create a logical vector


logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
print(logical_vector) # Output: TRUE FALSE TRUE TRUE
2. Using the ‘ : ‘operator:
# Create a sequence of integers
integer_vector <- 1:5
print(integer_vector) # Output: 1 2 3 4 5

# Create a sequence of characters


character_vector <- "a":"e"
print(character_vector) # Output: "a" "b" "c" "d" "e"
3. Using functions like ‘seq()’, ‘rep()’, or ‘vector()’:
# Create a sequence of numbers using seq()
sequence_vector <- seq(from = 1, to = 10, by = 2)
print(sequence_vector) # Output: 1 3 5 7 9

# Create a repeated vector using rep()


repeated_vector <- rep(0, times = 3)
print(repeated_vector) # Output: 0 0 0

# Create a vector of specific length using vector()


length_vector <- vector("numeric", length = 4)
print(length_vector) # Output: 0 0 0 0
Examples – Element-wise Operations :
1. Addition
# Addition of two numeric vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
result <- vector1 + vector2
print(result) # Output: 5 7 9

# Addition of two-character vectors


vector3 <- c("Hello", "World")
vector4 <- c(" ", "R!")
result2 <- vector3 + vector4
print(result2) # Output: "Hello " "WorldR!"
Examples – Element-wise Operations :
2. Subtraction
# Subtraction of two numeric vectors
vector1 <- c(5, 10, 15)
vector2 <- c(2, 3, 4)
result <- vector1 - vector2
print(result) # Output: 3 7 11

# Subtraction of two logical vectors


vector3 <- c(TRUE, FALSE, TRUE)
vector4 <- c(TRUE, TRUE, FALSE)
result2 <- vector3 - vector4
print(result2) # Output: 0 -1 1
Examples – Element-wise Operations :
3. Multiplication
# Multiplication of two numeric vectors
vector1 <- c(2, 4, 6)
vector2 <- c(3, 5, 2)
result <- vector1 * vector2
print(result) # Output: 6 20 12

# Multiplication of two logical vectors


vector3 <- c(TRUE, FALSE, TRUE)
vector4 <- c(FALSE, TRUE, FALSE)
result2 <- vector3 * vector4
print(result2) # Output: 0 0 0
Examples – Element-wise Operations :
4. Division
# Division of two numeric vectors
vector1 <- c(10, 20, 30)
vector2 <- c(2, 4, 5)
result <- vector1 / vector2
print(result) # Output: 5 5 6

# Division of two logical vectors


vector3 <- c(TRUE, FALSE, TRUE)
vector4 <- c(FALSE, TRUE, TRUE)
result2 <- vector3 / vector4
print(result2) # Output: Inf 0 1
Examples – Aggregation Functions:
1. Sum
# Sum of numeric vector
vector <- c(1, 2, 3, 4, 5)
sum_result <- sum(vector)
print(sum_result) # Output: 15

# Sum of logical vector


logical_vector <- c(TRUE, TRUE, FALSE)
logical_sum_result <- sum(logical_vector)
print(logical_sum_result) # Output: 2
Examples – Aggregation Functions:
2. Mean
# Mean of numeric vector
vector <- c(2, 4, 6, 8, 10)
mean_result <- mean(vector)
print(mean_result) # Output: 6

# Mean of logical vector


logical_vector <- c(TRUE, TRUE, FALSE)
logical_mean_result <- mean(logical_vector)
print(logical_mean_result) # Output: 0.6666667
Examples – Aggregation Functions:
3. Minimum
# Minimum value in numeric vector
vector <- c(5, 2, 9, 1, 7)
min_result <- min(vector)
print(min_result) # Output: 1

# Minimum value in character vector


character_vector <- c("apple", "banana", "orange")
min_character_result <- min(character_vector)
print(min_character_result) # Output: "apple"
Examples – Aggregation Functions:
4. Maximum
# Maximum value in numeric vector
vector <- c(5, 2, 9, 1, 7)
max_result <- max(vector)
print(max_result) # Output: 9

# Maximum value in character vector


character_vector <- c("apple", "banana", "orange")
max_character_result <- max(character_vector)
print(max_character_result) # Output: "orange"
Examples – Aggregation Functions:
5. Median
# Median of numeric vector
vector <- c(1, 2, 3, 4, 5)
median_result <- median(vector)
print(median_result) # Output: 3

# Median of logical vector


logical_vector <- c(TRUE, TRUE, FALSE)
logical_median_result <- median(logical_vector)
print(logical_median_result) # Output: 1
Examples – Vector Indexing and Slicing:
1. Vector Indexing
# Accessing individual elements of a vector
vector <- c(1, 2, 3, 4, 5)
element <- vector[3]
print(element) # Output: 3

# Modifying individual elements of a vector


vector[5] <- 10
print(vector) # Output: 1 2 3 4 10

# Accessing multiple elements of a vector using indices


vector <- c(1, 2, 3, 4, 5)
subset_vector <- vector[c(2, 4)]
print(subset_vector) # Output: 2 4
# Accessing elements of a vector using logical indexing
vector <- c(1, 2, 3, 4, 5)
logical_indices <- c(FALSE, TRUE, FALSE, TRUE, FALSE)
subset_vector <- vector[logical_indices]
print(subset_vector) # Output: 2 4
Examples – Vector Indexing and Slicing:
1. Vector Slicing
# Slicing a vector using indices
vector <- c(1, 2, 3, 4, 5)
slice_vector <- vector[2:4]
print(slice_vector) # Output: 2 3 4

# Slicing a vector using logical conditions


vector <- c(1, 2, 3, 4, 5)
subset_vector <- vector[vector > 2]
print(subset_vector) # Output: 3 4 5
Examples – Vector Comparison
1. Equal to
# Element-wise equality comparison of numeric vectors
vector1 <- c(1, 2, 3)
vector2 <- c(3, 2, 1)
result <- vector1 == vector2
print(result) # Output: FALSE TRUE FALSE

# Element-wise equality comparison of character vectors


vector3 <- c("apple", "banana", "orange")
vector4 <- c("apple", "grape", "orange")
result2 <- vector3 == vector4
print(result2) # Output: TRUE FALSE TRUE
Examples – Vector Comparison
2. Not Equal to
# Element-wise inequality comparison of numeric vectors
vector1 <- c(1, 2, 3)
vector2 <- c(3, 2, 1)
result <- vector1 != vector2
print(result) # Output: TRUE FALSE TRUE

# Element-wise inequality comparison of logical vectors


vector3 <- c(TRUE, FALSE, TRUE)
vector4 <- c(TRUE, TRUE, FALSE)
result2 <- vector3 != vector4
print(result2) # Output: FALSE TRUE TRUE
Examples – Vector Comparison
3. Greater than
# Element-wise greater than comparison of numeric vectors
vector1 <- c(1, 2, 3)
vector2 <- c(2, 1, 4)
result <- vector1 > vector2
print(result) # Output: FALSE TRUE FALSE

# Element-wise greater than comparison of character vectors


vector3 <- c("apple", "banana", "orange")
vector4 <- c("apple", "grape", "orange")
result2 <- vector3 > vector4
print(result2) # Output: FALSE TRUE FALSE
Examples – Vector Comparison
3. Lesser than
# Element-wise less than comparison of numeric vectors
vector1 <- c(1, 2, 3)
vector2 <- c(2, 1, 4)
result <- vector1 < vector2
print(result) # Output: TRUE FALSE TRUE

# Element-wise less than comparison of logical vectors


vector3 <- c(TRUE, FALSE, TRUE)
vector4 <- c(TRUE, TRUE, FALSE)
result2 <- vector3 < vector4
print(result2) # Output: FALSE TRUE FALSE
Examples – Vector Functions and Transformations
1. Square Root
# Square root of a numeric vector
vector <- c(4, 9, 16)
result <- sqrt(vector)
print(result) # Output: 2 3 4

# Square root of a numeric vector element-wise


vector2 <- c(2, 4, 6)
result2 <- sqrt(vector2)
print(result2) # Output: 1.414214 2 2.44949
Examples – Vector Functions and Transformations
2. Exponential
# Exponential of a numeric vector
vector <- c(1, 2, 3)
result <- exp(vector)
print(result) # Output: 2.718282 7.389056 20.085537

# Exponential of a numeric vector element-wise


vector2 <- c(2, 4, 6)
result2 <- exp(vector2)
print(result2) # Output: 7.389056 54.598150 403.428793
Examples – Vector Functions and Transformations
3. Logarithm
# Natural logarithm of a numeric vector
vector <- c(1, 10, 100)
result <- log(vector)
print(result) # Output: 0 2.302585 4.60517

# Natural logarithm of a numeric vector element-wise


vector2 <- c(2, 4, 6)
result2 <- log(vector2)
print(result2) # Output: 0.6931472 1.3862944 1.7917595
Examples – Vector Functions and Transformations
4. Sorting
# Sorting a numeric vector in ascending order
vector <- c(5, 2, 9, 1, 7)
sorted_vector <- sort(vector)
print(sorted_vector) # Output: 1 2 5 7 9

# Sorting a character vector in alphabetical order


vector2 <- c("banana", "apple", "orange")
sorted_vector2 <- sort(vector2)
print(sorted_vector2) # Output: "apple" "banana" "orange"
Recursive Vectors (Lists)
• Lists are a step up in complexity from atomic
vectors, because lists can contain other lists.
Visualizing Lists
• x1 <- list(c(1, 2), c(3, 4))
• x2 <- list(list(1, 2), list(3, 4))
• x3 <- list(1, list(2, list(3)))
Example1
• Write an R program to create two numeric
vectors "heights" and "weights" with values
(170, 175, 180, 165, 185) and (65, 70, 75, 60,
80) respectively. Calculate the BMI (Body
Mass Index) for each person and store it in a
new vector "bmi."
Code
heights <- c(170, 175, 180, 165, 185)
weights <- c(65, 70, 75, 60, 80)
bmi <- weights / ((heights / 100) ^ 2)
print(bmi)
Example2
• Write an R program to create a numeric
vector "grades" with values (85, 92, 78, 95,
88, 70) representing students' test scores.
Calculate and print the average (mean) grade.
Code
grades <- c(85, 92, 78, 95, 88, 70)
average_grade <- mean(grades)
print(average_grade)

You might also like