Operations in detail
Vectors are the fundamental data types in R. Any object created is stored as a vector by default. A vector is essentially a sequence of homogeneous data elements, similar to arrays in other programming languages. When a vector contains mixed data types, R automatically converts the elements to the type with the highest precedence.
Numerous operations can be performed on vectors in R, such as creation, access, modification, deletion, arithmetic operations, and sorting.
Creating a Vector
Vectors can be created in several ways, the most common being the c() function, which combines elements into a vector.
# Using 'c()' to combine values into a vector
# The type defaults to double
vec1 <- c(3, 8, 1, 9, 4)
print('Vector using c():')
print(vec1)
# Using seq() to create a sequence with specified step size and length
vec2 <- seq(2, 12, length.out = 4)
print('Vector using seq():')
print(vec2)
# Using ':' operator for continuous sequences
vec3 <- 10:15
print('Vector using colon:')
print(vec3)
Accessing Vector Elements
In R, vector elements are accessed using square brackets []. R follows a 1-based indexing system, unlike programming languages like C or Python where indexing starts from 0.
# Accessing elements using position
vec1 <- c(5, 9, 2, 6, 3)
print('Accessing second element:')
print(vec1[2])
# Accessing specific elements using another vector
vec2 <- c(4, 7, 1, 8, 5)
print('Accessing specific elements:')
print(vec2[c(1, 4)])
# Logical indexing to filter elements
vec3 <- c(6, 2, 8, 3, 7)
print('Elements greater than 5:')
print(vec3[vec3 > 5])
Output:
Accessing second element:
[1] 9
Accessing specific elements:
[1] 4 8
Elements greater than 5:
[1] 6 8 7
Modifying a Vector
Vectors can be modified using indexing or logical operations.
# Creating a vector
vec <- c(4, 7, 1, 9, 3)
# Modifying a specific element
vec[3] <- 10
print('Modified third element:')
print(vec)
# Logical indexing to modify elements
vec[vec > 8] <- 0
print('Replaced elements greater than 8:')
print(vec)
# Rearranging the vector using positions
vec <- vec[c(5, 1, 2)]
print('Rearranged vector:')
print(vec)
Output:
Modified third element:
[1] 4 7 10 9 3
Replaced elements greater than 8:
[1] 4 7 10 0 3
Rearranged vector:
[1] 3 4 7
Deleting a Vector
To delete a vector, it can be reassigned to NULL.
# Creating a vector
vec <- c(3, 6, 1, 8)
# Deleting the vector
vec <- NULL
print('Deleted vector:')
print(vec)
Output:
Deleted vector:
NULL
Arithmetic Operations
Arithmetic operations on vectors are performed element-wise. Both vectors should have the same length.
# Creating two vectors
vec1 <- c(6, 3, 9, 1, 7)
vec2 <- c(2, 5, 4, 8, 3)
# Addition
add <- vec1 + vec2
print('Addition:')
print(add)
# Subtraction
sub <- vec1 - vec2
print('Subtraction:')
print(sub)
# Multiplication
mul <- vec1 * vec2
print('Multiplication:')
print(mul)
# Division
div <- vec1 / vec2
print('Division:')
print(div)
Output:
Addition:
[1] 8 8 13 9 10
Subtraction:
[1] 4 -2 5 -7 4
Multiplication:
[1] 12 15 36 8 21
Division:
[1] 3.000000 0.600000 2.250000 0.125000 2.333333
Sorting a Vector
The sort() function is used to arrange vector elements. By default, sorting is done in ascending order.
# Creating a vector
vec <- c(9, 3, 7, 1, 8, 2)
# Sorting in ascending order
asc <- sort(vec)
print('Ascending order:')
print(asc)
# Sorting in descending order
desc <- sort(vec, decreasing = TRUE)
print('Descending order:')
print(desc)
Output:
Ascending order:
[1] 1 2 3 7 8 9
Descending order:
[1] 9 8 7 3 2 1
Leave a Reply