Common Vector Operations in R

Common Vector Operations in R

Vector operations are fundamental for data manipulation and analysis in R. These operations allow you to perform calculations and transformations on vectors efficiently. Here’s a comprehensive overview of common vector operations in R.

Basic Arithmetic Operations

You can perform basic arithmetic operations element-wise on vectors.

Examples: 

# Define two numeric vectors
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(10, 20, 30, 40, 50)
# Addition
sum_vec <- vec1 + vec2
print(sum_vec)  # Output: 11 22 33 44 55
# Subtraction
diff_vec <- vec1 - vec2
print(diff_vec)  # Output: -9 -18 -27 -36 -45
# Multiplication
prod_vec <- vec1 * vec2
print(prod_vec)  # Output: 10 40 90 160 250
# Division
div_vec <- vec1 / vec2
print(div_vec)  # Output: 0.1 0.1 0.1 0.1 0.1

Explanation:

Arithmetic operations are performed element-wise.

Logical Operations

Logical vectors enable comparisons and create boolean vectors.

Examples: 

# Define two numeric vectors
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(3, 4, 2, 5, 1)
# Equality comparison
eq_vec <- vec1 == vec2
print(eq_vec)  # Output: FALSE FALSE TRUE FALSE FALSE
# Greater than comparison
gt_vec <- vec1 > vec2
print(gt_vec)  # Output: FALSE FALSE TRUE FALSE TRUE
# Greater than or equal to comparison
ge_vec <- vec1 >= vec2
print(ge_vec)  # Output: FALSE FALSE TRUE TRUE TRUE

Explanation:

Logical comparisons return boolean vectors (TRUE or FALSE).

Summary Statistics

Summary operations compute simple statistics on vectors.

Examples: 

# Define a numeric vector
vec <- c(5, 10, 15, 20, 25)
# Sum of elements
sum_vec <- sum(vec)
print(sum_vec)  # Output: 75
# Mean of elements
mean_vec <- mean(vec)
print(mean_vec)  # Output: 15
# Variance of elements
var_vec <- var(vec)
print(var_vec)  # Output: 62.5
# Standard deviation of elements
sd_vec <- sd(vec)
print(sd_vec)  # Output: 7.905694

Explanation:

  • sum() calculates the sum of elements.
  • mean() computes the mean.
  • var() calculates variance.
  • sd() computes the standard deviation.

Sorting Operations 

Sorting vectors is crucial for data analysis.

Examples:

# Define a numeric vector
vec <- c(3, 1, 4, 1, 5, 9, 2)
# Sort elements in ascending order
sorted_vec <- sort(vec)
print(sorted_vec)  # Output: 1 1 2 3 4 5 9
# Sort elements in descending order
sorted_vec_desc <- sort(vec, decreasing = TRUE)
print(sorted_vec_desc)  # Output: 9 5 4 3 2 1 1

 Explanation:

  • sort() sorts vector elements.
  • decreasing = TRUE sorts in descending order.

Indexing Elements

Access elements of a vector using indices.

Examples: 

# Define a numeric vector
vec <- c(10, 20, 30, 40, 50)
# Access the first element
first_elem <- vec[1]
print(first_elem)  # Output: 10
# Access elements from index 2 to 4
sub_vec <- vec[2:4]
print(sub_vec)  # Output: 20 30 40
# Access specific elements
specific_elems <- vec[c(1, 3, 5)]
print(specific_elems)  # Output: 10 30 50

Explanation:

  • Indices are used to extract specific elements from the vector.

Modifying Elements

Modify elements of a vector using indices.

Examples: 

# Define a numeric vector
vec <- c(1, 2, 3, 4, 5)
# Modify the third element
vec[3] <- 99
print(vec)  # Output: 1 2 99 4 5
# Modify multiple elements
vec[c(1, 4)] <- c(10, 20)
print(vec)  # Output: 10 2 99 20 5

Explanation:

  • Indices are used to update specific elements in the vector.

Element Names Manipulation

Element names in a vector can be assigned and manipulated.

Examples: 

# Define a named vector
vec <- c(a = 1, b = 2, c = 3)
# Access an element by name
element_b <- vec["b"]
print(element_b)  # Output: 2
# Modify an element using its name
vec["c"] <- 99
print(vec)  # Output: a 1 b 2 c 99

Explanation:

  • Names of elements allow for more readable access and modification.

Combining and Stacking Vectors

Vectors can be combined or stacked to create new vectors or matrices.

Examples: 

# Define two vectors
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
# Combine vectors
combined_vec <- c(vec1, vec2)
print(combined_vec)  # Output: 1 2 3 4 5 6
# Stack vectors into a matrix
matrix_vec <- rbind(vec1, vec2)
print(matrix_vec)
# Output:
#      [,1] [,2] [,3]
# vec1    1    2    3
# vec2    4    5    6

Explanation:

  • c() combines multiple vectors.
  • rbind() stacks vectors into rows of a matrix.

Vector Calculations

Perform cumulative calculations like cumulative sums or products.

Examples: 

# Define a numeric vector
vec <- c(1, 2, 3, 4, 5)
# Cumulative sum
cumsum_vec <- cumsum(vec)
print(cumsum_vec)  # Output: 1 3 6 10 15
# Cumulative product
cumprod_vec <- cumprod(vec)
print(cumprod_vec)  # Output: 1 2 6 24 120

Explanation:

  • cumsum() calculates the cumulative sum.
  • cumprod() calculates the cumulative product.

Summary

Common vector operations in R include arithmetic, logical operations, summary statistics, sorting, indexing, modification, name manipulation, combining and stacking vectors, and cumulative calculations. Mastering these operations allows for efficient and effective data manipulation and analysis.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *