R Vectors

R Vectors

Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com


Table of Contents

  1. Introduction to Vectors in R

  2. Creating Vectors

    • Using c() Function

    • Using seq() and rep() Functions

  3. Accessing Elements in a Vector

  4. Vector Operations

    • Arithmetic Operations

    • Relational Operations

    • Logical Operations

  5. Vector Functions

    • length()

    • sum()

    • mean()

    • sort()

  6. Modifying Vectors

    • Adding Elements

    • Removing Elements

    • Updating Elements

  7. Named Vectors

  8. Combining Vectors

  9. Vector Coercion


1. Introduction to Vectors in R

Vectors are one of the most basic and essential data structures in R. A vector is a sequence of data elements that are of the same type. Vectors can hold numeric, character, logical, or complex data types. In R, almost everything is a vector, making it a fundamental concept for R programming.


2. Creating Vectors

2.1 Using c() Function

The simplest way to create a vector in R is by using the c() (combine) function.

Example:

# Creating a numeric vector
num_vec <- c(1, 2, 3, 4, 5)

# Creating a character vector
char_vec <- c("A", "B", "C")

# Creating a logical vector
log_vec <- c(TRUE, FALSE, TRUE)

2.2 Using seq() and rep() Functions

  • seq(): Creates a sequence of numbers.

  • rep(): Repeats elements of a vector.

Example:

# Sequence from 1 to 10
seq_vec <- seq(1, 10)

# Repeating elements
rep_vec <- rep(1:3, times = 3)

3. Accessing Elements in a Vector

You can access elements in a vector using square brackets []. R uses 1-based indexing, meaning the first element is at position 1.

Example:

# Accessing elements in a vector
num_vec <- c(10, 20, 30, 40, 50)

# Access the first element
print(num_vec[1])  # Output: 10

# Access multiple elements
print(num_vec[1:3])  # Output: 10 20 30

4. Vector Operations

4.1 Arithmetic Operations

You can perform arithmetic operations on vectors, and the operations are applied element-wise.

Example:

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

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

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

4.2 Relational Operations

Relational operations are also applied element-wise and return a logical vector.

Example:

vec <- c(10, 20, 30)

# Greater than comparison
print(vec > 15)  # Output: FALSE TRUE TRUE

4.3 Logical Operations

Logical operations can be used to combine conditions across vector elements.

Example:

vec <- c(TRUE, FALSE, TRUE)

# Logical AND
print(vec & c(TRUE, TRUE, FALSE))  # Output: TRUE FALSE FALSE

# Logical OR
print(vec | c(FALSE, FALSE, TRUE))  # Output: TRUE FALSE TRUE

5. Vector Functions

5.1 length()

The length() function returns the number of elements in a vector.

Example:

vec <- c(1, 2, 3, 4, 5)
print(length(vec))  # Output: 5

5.2 sum()

The sum() function returns the sum of all elements in a numeric vector.

Example:

vec <- c(1, 2, 3, 4, 5)
print(sum(vec))  # Output: 15

5.3 mean()

The mean() function returns the average of the elements in a numeric vector.

Example:

vec <- c(1, 2, 3, 4, 5)
print(mean(vec))  # Output: 3

5.4 sort()

The sort() function sorts the elements of a vector in ascending or descending order.

Example:

vec <- c(5, 2, 3, 1, 4)
print(sort(vec))  # Output: 1 2 3 4 5

6. Modifying Vectors

6.1 Adding Elements

You can add elements to a vector using the c() function.

Example:

vec <- c(1, 2, 3)
vec <- c(vec, 4)
print(vec)  # Output: 1 2 3 4

6.2 Removing Elements

To remove elements from a vector, you can use negative indexing.

Example:

vec <- c(1, 2, 3, 4)
vec <- vec[-2]  # Removes the second element
print(vec)  # Output: 1 3 4

6.3 Updating Elements

You can update elements in a vector by directly assigning new values to specific positions.

Example:

vec <- c(1, 2, 3)
vec[2] <- 10
print(vec)  # Output: 1 10 3

7. Named Vectors

You can assign names to the elements of a vector, making it easier to reference specific elements.

Example:

vec <- c(1, 2, 3)
names(vec) <- c("First", "Second", "Third")

# Access elements by name
print(vec["Second"])  # Output: 2

8. Combining Vectors

You can combine vectors using the c() function to create larger vectors.

Example:

vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
combined_vec <- c(vec1, vec2)
print(combined_vec)  # Output: 1 2 3 4 5 6

9. Vector Coercion

If you create a vector with elements of different types, R will automatically coerce them into a common type. The hierarchy of coercion is logical -> integer -> numeric -> character.

Example:

vec <- c(1, "two", 3)
print(vec)  # Output: "1" "two" "3"
print(class(vec))  # Output: "character"

Conclusion

Vectors are a fundamental data structure in R, and mastering their creation, manipulation, and operations is essential for effective R programming. Whether you're working with numeric, logical, or character data, vectors provide a flexible and powerful way to store and manipulate data.

For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.

Last updated