Week 3: Manipulating Data with Vectors & Matrices

Learn indexing, slicing, recycling, and introduce matrices and R's help system.

Explore Chapter 3

Chapter 3: Data Structures - Vectors & Matrices

Vector Operations: Indexing, Slicing, Recycling.

Last week, we introduced vectors as ordered collections of elements of the same type. Now, let's learn how to access and manipulate these elements.

Vector Indexing

You can select specific elements from a vector using square brackets `[]`. R uses 1-based indexing, meaning the first element is at index 1.

  • Positive Integers: Select elements at the specified positions.
    my_vector <- c(10, 20, 30, 40, 50)
    my_vector[1]       # Output: [1] 10 (First element)
    my_vector[3]       # Output: [1] 30 (Third element)
    my_vector[c(1, 4)] # Output: [1] 10 40 (First and fourth elements)
  • Negative Integers: Exclude elements at the specified positions.
    my_vector[-1]      # Output: [1] 20 30 40 50 (All except the first)
    my_vector[-c(2, 3)] # Output: [1] 10 40 50 (All except second and third)
  • Logical Vectors: Select elements where the corresponding logical value is `TRUE`. The logical vector must typically be the same length as the vector being indexed.
    my_vector[c(TRUE, FALSE, TRUE, FALSE, TRUE)] # Output: [1] 10 30 50
    # Often used with conditions:
    my_vector[my_vector > 25] # Output: [1] 30 40 50

Vector Slicing

You can select a contiguous sequence of elements using the colon operator `:` to create an integer sequence for indexing.

my_vector[2:4] # Output: [1] 20 30 40 (Elements at index 2, 3, and 4)

Vector Recycling

When you perform an operation involving two vectors of different lengths, R automatically recycles (repeats) the elements of the shorter vector to match the length of the longer one. This can be useful, but be aware of it to avoid unexpected results, especially if the length of the longer vector is not a multiple of the shorter one (which usually results in a warning).

vec_long <- c(1, 2, 3, 4, 5, 6)
vec_short <- c(10, 20)

vec_long + vec_short  # Output: [1] 11 22 13 24 15 26
# vec_short is recycled to c(10, 20, 10, 20, 10, 20) before addition

vec_short2 <- c(10, 20, 30)
vec_long + vec_short2 # Output: [1] 11 22 33 14 25 36
# Output includes a warning because 6 is not a multiple of 3
# Warning message:
# In vec_long + vec_short2 : longer object length is not a multiple of shorter object length

Introduction to Matrices.

A matrix is a two-dimensional rectangular data structure in R. Like vectors, all elements in a matrix must be of the same data type (e.g., all numeric or all character).

Creating Matrices

Matrices are typically created using the `matrix()` function:

# Create a matrix from a vector of data
data_vector <- 1:6  # Creates vector c(1, 2, 3, 4, 5, 6)

# Create a 2x3 matrix (2 rows, 3 columns), filled by column (default)
mat1 <- matrix(data = data_vector, nrow = 2, ncol = 3)
print(mat1)
# Output:
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6

# Create a 2x3 matrix, filled by row
mat2 <- matrix(data = data_vector, nrow = 2, ncol = 3, byrow = TRUE)
print(mat2)
# Output:
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

Matrix Indexing

You access elements in a matrix using two indices inside square brackets: `matrix[row, column]`. Leaving an index blank selects the entire row or column.

# Using mat2 from above
mat2[1, 2]  # Output: [1] 2 (Element at row 1, column 2)
mat2[2, 3]  # Output: [1] 6 (Element at row 2, column 3)

mat2[1, ]    # Output: [1] 1 2 3 (Selects the entire first row)
mat2[, 3]    # Output: [1] 3 6 (Selects the entire third column)

mat2[1, c(1, 3)] # Output: [1] 1 3 (Row 1, columns 1 and 3)

Matrices are fundamental for many mathematical and statistical operations in R, especially linear algebra.

Getting Help in R.

As you learn R, knowing how to find help is essential. R has excellent built-in documentation and functions to assist you.

  • `help(function_name)` or `?function_name`: The most common way to get help. This displays the documentation page for a specific function or topic if you know its name.
    help(mean)
    ?matrix
    ?c

    The help page typically includes a description, usage details, argument explanations, examples, and related functions.

  • `help.search("keyword")` or `??keyword`: Searches the documentation for a keyword or concept when you don't know the exact function name. Requires quotes for multi-word searches.
    help.search("standard deviation")
    ??"linear model"

    This returns a list of potentially relevant help pages.

  • `example(function_name)`: Runs the example code provided in the help documentation for a specific function. This is a great way to see the function in action.
    example(mean)
    example(plot)
  • `str(object_name)`: Displays the structure of an R object (like a vector, matrix, data frame, or list), showing its type, dimensions, and a preview of its contents. Very useful for understanding complex objects.
    my_vec <- c("a", "b", "c")
    str(my_vec) # Output: chr [1:3] "a" "b" "c"
    
    my_mat <- matrix(1:4, nrow=2)
    str(my_mat) # Output: int [1:2, 1:2] 1 2 3 4

Don't hesitate to use these help functions frequently! They are invaluable tools for learning and troubleshooting in R.

Syllabus