Vector Arithmetic and Logical Operations with R

Vector Arithmetic and Logical Operations

Vector Arithmetic Operations

Vector arithmetic operations allow you to perform mathematical calculations element-wise between vectors or with scalar values. This is essential for data analysis and manipulation.

Addition, Subtraction, Multiplication, and Division

These operations are performed element-wise.

Examples: 

# Define two numeric vectors
vec1 <- c(2, 4, 6, 8)
vec2 <- c(1, 3, 5, 7)
# Addition
add_vec <- vec1 + vec2
print(add_vec)  # Output: 3 7 11 15
# Subtraction
sub_vec <- vec1 - vec2
print(sub_vec)  # Output: 1 1 1 1
# Multiplication
mul_vec <- vec1 * vec2
print(mul_vec)  # Output: 2 12 30 56
# Division
div_vec <- vec1 / vec2
print(div_vec)  # Output: 2 1.333333 1.2 1.142857

Explanation:

  • Each element in vec1 is combined with the corresponding element in vec2 using the specified arithmetic operation.

Scalar Arithmetic Operations

When you perform arithmetic operations with a scalar and a vector, the scalar is applied to each element of the vector.

Examples: 

# Define a numeric vector
vec <- c(5, 10, 15)
# Addition with a scalar
add_scalar <- vec + 2
print(add_scalar)  # Output: 7 12 17
# Multiplication with a scalar
mul_scalar <- vec * 3
print(mul_scalar)  # Output: 15 30 45

Explanation:

  • The scalar 2 or 3 is added or multiplied with each element of vec.

Logical Operations on Vectors

Logical operations are used to perform comparisons and generate boolean vectors (TRUE or FALSE).

Basic Comparisons

Examples: 

# Define two numeric vectors
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(3, 2, 1, 4, 6)
# Equality comparison
eq_logical <- vec1 == vec2
print(eq_logical)  # Output: FALSE TRUE FALSE TRUE FALSE
# Greater than comparison
gt_logical <- vec1 > vec2
print(gt_logical)  # Output: FALSE FALSE TRUE FALSE FALSE
# Less than comparison
lt_logical <- vec1 < vec2
print(lt_logical)  # Output: TRUE FALSE FALSE FALSE TRUE

Explanation:

  • == checks if elements are equal.
  • > and < check for greater than or less than conditions, respectively.

Logical Operators

Logical operators like & (and), | (or), and ! (not) can be used to combine or invert logical vectors.

Examples: 

# Define two logical vectors
log_vec1 <- c(TRUE, FALSE, TRUE, FALSE)
log_vec2 <- c(FALSE, FALSE, TRUE, TRUE)
# Logical AND
and_logical <- log_vec1 & log_vec2
print(and_logical)  # Output: FALSE FALSE TRUE FALSE
# Logical OR
or_logical <- log_vec1 | log_vec2
print(or_logical)  # Output: TRUE FALSE TRUE TRUE
# Logical NOT
not_logical <- !log_vec1
print(not_logical)  # Output: FALSE TRUE FALSE TRUE

Explanation:

  • & performs element-wise logical AND.
  • | performs element-wise logical OR.
  • ! inverts the boolean values.

Combining Logical Conditions

You can use logical operators to combine multiple conditions.

Examples: 

# Define a numeric vector
vec <- c(1, 4, 5, 6, 7, 9)
# Logical conditions
condition1 <- vec > 3
condition2 <- vec %% 2 == 0
# Combine conditions
combined_condition <- condition1 & condition2
print(combined_condition)  # Output: FALSE TRUE FALSE TRUE FALSE FALSE

Explanation:

  • condition1 checks if elements are greater than 3.
  • condition2 checks if elements are even.
  • combined_condition combines both conditions using &.

Using Logical Vectors for Subsetting

Logical vectors can be used to subset other vectors or data frames.

Examples: 

# Define a numeric vector
vec <- c(10, 20, 30, 40, 50)
# Define a logical vector
logical_subset <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
# Subset the numeric vector
subset_vec <- vec[logical_subset]
print(subset_vec)  # Output: 10 30 50

Explanation:

  • logical_subset is used to filter vec. Only elements corresponding to TRUE are included.

Handling NA and NULL Values in Arithmetic and Logical Operations

  • NA (Not Available): Represents missing or undefined values in R. Operations involving NA generally result in NA.
  • NULL: Represents the absence of a value or an empty object. NULL values can disrupt operations and are typically used for different purposes.

Examples: 

# Define vectors with NA values
vec1 <- c(1, 2, NA, 4)
vec2 <- c(5, NA, 3, 2)
# Arithmetic operation with NA
na_sum <- vec1 + vec2
print(na_sum)  # Output: 6 NA NA 6
# Logical operation with NA
na_comparison <- vec1 == vec2
print(na_comparison)  # Output: FALSE NA NA FALSE
# Handling NULL values
null_vec <- NULL
print(null_vec + vec1)  # Output: NULL

Explanation:

  • Arithmetic and logical operations involving NA typically result in NA.
  • NULL does not interact well with arithmetic operations and generally propagates through operations.

Summary

Vector arithmetic operations in R allow for element-wise computations between vectors or with scalars, including addition, subtraction, multiplication, and division. Logical operations enable comparisons and the creation of boolean vectors, with operators like ==, >, <, &, |, and ! facilitating various logical tests. Logical vectors are useful for subsetting and filtering. Handling NA and NULL values requires special attention, as they affect the results of arithmetic and logical operations.

Laisser un commentaire

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