R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Operations on Vectors in R

Vectors are the most basic data structure in R. They contain elements of the same type (e.g., numeric, character, or logical). This tutorial will walk you through various operations that can be performed on vectors in R.

1. Creating Vectors:

You can create vectors using the c() function:

vec <- c(1, 2, 3, 4, 5)
print(vec)

2. Accessing Vector Elements:

You can access specific elements using indices:

print(vec[2])    # Prints the second element
print(vec[c(1,4)]) # Prints the first and fourth elements

3. Modifying Vectors:

vec[1] <- 10         # Modify the first element
vec[c(2,3)] <- c(11,12) # Modify the second and third elements

4. Vector Arithmetic:

vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)

print(vec1 + vec2)  # Element-wise addition
print(vec1 - vec2)  # Element-wise subtraction
print(vec1 * vec2)  # Element-wise multiplication
print(vec1 / vec2)  # Element-wise division

5. Vector Functions:

  • Length of a vector: length(vec)
  • Sum of vector elements: sum(vec)
  • Product of vector elements: prod(vec)
  • Mean of vector elements: mean(vec)
  • Variance of vector elements: var(vec)
  • Standard deviation: sd(vec)

6. Logical Operations:

print(vec1 > 2)    # Returns a logical vector: FALSE FALSE TRUE
print(vec1 == 2)   # Returns: FALSE TRUE FALSE

7. Vectorized Operations:

Many functions in R are vectorized, meaning they operate element-wise on vectors:

print(sqrt(vec1)) # Square root of each element
print(abs(-vec1)) # Absolute value of each element

8. Sequences and Repetition:

  • Generating sequences:

    seq_vec <- seq(1, 10, by=2)
    
  • Repeating vectors:

    rep_vec <- rep(vec1, times=3)
    

9. Naming Vector Elements:

You can assign names to vector elements:

names(vec1) <- c("first", "second", "third")
print(vec1)

10. Combining Vectors:

Combine vectors using the c() function:

combined_vec <- c(vec1, vec2)

11. Sorting Vectors:

sorted_vec <- sort(vec, decreasing=TRUE)  # For descending order

12. Factorizing Vectors:

For categorical variables, you can create factors:

gender <- c("Male", "Female", "Male", "Female")
factor_gender <- as.factor(gender)
print(factor_gender)

Conclusion:

Vectors are a foundational data structure in R. Understanding how to create, manipulate, and perform operations on vectors is crucial for data analysis in R. Given their simplicity and power, vectors are often a starting point for many data manipulation tasks before proceeding to more complex data structures like data frames or matrices.

  1. Element-wise operations on vectors in R:

    • Overview: Introduce element-wise operations on vectors, such as squaring each element.

    • Code:

      # Element-wise operations on vectors
      my_vector <- c(1, 2, 3, 4)
      squared_vector <- my_vector^2
      
      # Printing the squared vector
      print("Squared Vector:")
      print(squared_vector)
      
  2. R vector addition and subtraction:

    • Overview: Demonstrate vector addition and subtraction in R.

    • Code:

      # Vector addition and subtraction
      vector1 <- c(1, 2, 3)
      vector2 <- c(4, 5, 6)
      
      # Vector addition
      sum_vector <- vector1 + vector2
      
      # Vector subtraction
      diff_vector <- vector1 - vector2
      
      # Printing the results
      print("Vector1:")
      print(vector1)
      print("Vector2:")
      print(vector2)
      print("Vector Addition:")
      print(sum_vector)
      print("Vector Subtraction:")
      print(diff_vector)
      
  3. Multiplying vectors in R programming:

    • Overview: Illustrate vector multiplication, both element-wise and dot product.

    • Code:

      # Multiplying vectors in R
      vector1 <- c(1, 2, 3)
      vector2 <- c(4, 5, 6)
      
      # Element-wise multiplication
      elementwise_product <- vector1 * vector2
      
      # Dot product
      dot_product <- sum(vector1 * vector2)
      
      # Printing the results
      print("Element-wise Product:")
      print(elementwise_product)
      print("Dot Product:")
      print(dot_product)
      
  4. Vectorized operations in R with apply functions:

    • Overview: Discuss vectorized operations using apply functions like sapply.

    • Code:

      # Vectorized operations using apply functions
      my_vector <- c(1, 2, 3, 4)
      
      # Squaring each element using sapply
      squared_vector <- sapply(my_vector, function(x) x^2)
      
      # Printing the squared vector
      print("Squared Vector (sapply):")
      print(squared_vector)
      
  5. Sorting and filtering vectors in R:

    • Overview: Demonstrate sorting and filtering operations on vectors.

    • Code:

      # Sorting and filtering vectors in R
      my_vector <- c(3, 1, 4, 1, 5, 9, 2, 6)
      
      # Sorting in ascending order
      sorted_vector <- sort(my_vector)
      
      # Filtering values greater than 4
      filtered_vector <- my_vector[my_vector > 4]
      
      # Printing the results
      print("Original Vector:")
      print(my_vector)
      print("Sorted Vector:")
      print(sorted_vector)
      print("Filtered Vector:")
      print(filtered_vector)
      
  6. Vector concatenation and splitting in R:

    • Overview: Showcase vector concatenation and splitting using c() and split() functions.

    • Code:

      # Vector concatenation and splitting in R
      vector1 <- c(1, 2, 3)
      vector2 <- c(4, 5, 6)
      
      # Concatenating vectors
      concatenated_vector <- c(vector1, vector2)
      
      # Splitting concatenated vector
      split_vector <- split(concatenated_vector, rep(1:2, each = 3))
      
      # Printing the results
      print("Vector1:")
      print(vector1)
      print("Vector2:")
      print(vector2)
      print("Concatenated Vector:")
      print(concatenated_vector)
      print("Split Vector:")
      print(split_vector)