R Vectors

Introduction

In this chapter, you will learn about vectors in R. Vectors are the most basic and fundamental data structures in R. They are one-dimensional arrays that can hold numeric, character, or logical data. Understanding how to create, manipulate, and perform operations on vectors is essential for effective data analysis and programming in R.

Creating Vectors

Vectors can be created using the c() function, which combines multiple values into a single vector.

Example: Creating Numeric, Character, and Logical Vectors

Example:

# Numeric vector
num_vec <- c(1, 2, 3, 4, 5)
print(num_vec)

# Character vector
char_vec <- c("a", "b", "c", "d", "e")
print(char_vec)

# Logical vector
log_vec <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
print(log_vec)

Accessing Elements in Vectors

You can access elements in a vector using square brackets [] and specifying the index of the element you want to access. In R, indexing starts at 1.

Example: Accessing Elements in a Vector

Example:

# Accessing elements
print(num_vec[1])  # Output: 1
print(char_vec[2])  # Output: "b"
print(log_vec[3])  # Output: TRUE

You can also access multiple elements by providing a vector of indices.

Example:

# Accessing multiple elements
print(num_vec[c(1, 3, 5)])  # Output: 1 3 5

Modifying Elements in Vectors

You can modify elements in a vector by specifying the index and assigning a new value.

Example: Modifying Elements in a Vector

Example:

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

char_vec[2] <- "z"
print(char_vec)  # Output: "a" "z" "c" "d" "e"

Vector Operations

Vectors support element-wise operations such as addition, subtraction, multiplication, and division.

Example: Vector Operations

Example:

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

# Addition
print(vec1 + vec2)  # Output: 5 7 9

# Subtraction
print(vec1 - vec2)  # Output: -3 -3 -3

# Multiplication
print(vec1 * vec2)  # Output: 4 10 18

# Division
print(vec1 / vec2)  # Output: 0.25 0.4 0.5

Vector Functions

R provides several built-in functions for working with vectors, such as length(), sum(), mean(), max(), and min().

Example: Using Vector Functions

Example:

# Vector functions
vec <- c(1, 2, 3, 4, 5)

# Length of the vector
print(length(vec))  # Output: 5

# Sum of the elements
print(sum(vec))  # Output: 15

# Mean of the elements
print(mean(vec))  # Output: 3

# Maximum value
print(max(vec))  # Output: 5

# Minimum value
print(min(vec))  # Output: 1

Subsetting Vectors

Subsetting vectors allows you to extract specific elements based on conditions.

Example: Subsetting Vectors

Example:

# Subsetting vectors
vec <- c(1, 2, 3, 4, 5)

# Subset elements greater than 3
subset_vec <- vec[vec > 3]
print(subset_vec)  # Output: 4 5

# Subset elements that are even
even_vec <- vec[vec %% 2 == 0]
print(even_vec)  # Output: 2 4

Example Program Using Vectors

Here is an example program that demonstrates the use of vectors in R:

# R Program to Demonstrate Vectors

# Creating vectors
num_vec <- c(1, 2, 3, 4, 5)
char_vec <- c("a", "b", "c", "d", "e")
log_vec <- c(TRUE, FALSE, TRUE, TRUE, FALSE)

# Accessing elements
print(num_vec[1])  # Output: 1
print(char_vec[2])  # Output: "b"
print(log_vec[3])  # Output: TRUE

# Modifying elements
num_vec[1] <- 10
char_vec[2] <- "z"

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

add_result <- vec1 + vec2
sub_result <- vec1 - vec2
mul_result <- vec1 * vec2
div_result <- vec1 / vec2

# Print vector operations results
print(add_result)  # Output: 5 7 9
print(sub_result)  # Output: -3 -3 -3
print(mul_result)  # Output: 4 10 18
print(div_result)  # Output: 0.25 0.4 0.5

# Using vector functions
length_vec <- length(num_vec)
sum_vec <- sum(num_vec)
mean_vec <- mean(num_vec)
max_vec <- max(num_vec)
min_vec <- min(num_vec)

# Print vector function results
print(length_vec)  # Output: 5
print(sum_vec)  # Output: 24
print(mean_vec)  # Output: 4.8
print(max_vec)  # Output: 10
print(min_vec)  # Output: 2

# Subsetting vectors
subset_vec <- num_vec[num_vec > 3]
even_vec <- num_vec[num_vec %% 2 == 0]

# Print subsets
print(subset_vec)  # Output: 10 4 5
print(even_vec)  # Output: 10 4

Conclusion

In this chapter, you learned about vectors in R, including how to create, access, modify, and perform operations on vectors. You also learned about various vector functions and how to subset vectors. Vectors are fundamental data structures in R, and mastering them is essential for effective data analysis and programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top