📚 R Collections

Vectors are one-dimensional collections where all elements are of the same data type(numeric, character, logical, etc.). You create them with the c() function, which stands for “combine” or “concatenate.”

  • Indexing starts at 1 in R, not 0 like many other languages.
  • Operations on vectors are vectorized, meaning they apply element-wise by default.
  • Mixing types will coerce all elements to a common type (e.g., numeric + string becomes character).

Vectors

# Numeric vector
nums <- c(1, 2, 3)

# Character vector
words <- c("apple", "banana")

Lists

Lists are versatile containers that can hold elements of different types(numeric, character, logical, vectors, other lists, and more).

  • Elements can be named, which allows for easy reference using $ syntax.
  • Lists are useful for grouping related but non-uniform data together, such as model results or configurations.
  • To access nested elements, you can use double brackets [[ ]] or the $ operator for named items.
myList <- list(name = "Alice", age = 30, scores = c(85, 90, 95))

Matrices

Matrices are two-dimensional data structures where all elements must be of the same type. They are often used for numeric computations, linear algebra, and statistical models.

  • Created using matrix(), specifying nrow and/or ncol.
  • Filled column-wise by default; use byrow = TRUE to fill row-wise.
  • Elements are accessed using two indices: [row, column].
m <- matrix(1:6, nrow = 2, ncol = 3)

Data Frames

Data frames are table-like structures where columns can be of different types(numeric, character, logical, factor, etc.), but each column’s elements share the same type.

  • Think of them as similar to spreadsheets or SQL tables.
  • Data frames are the most common way to store datasets in R.
  • Use str(df) to see the structure, and summary(df) for summary statistics.
df <- data.frame(
  name = c("Alice", "Bob"),
  age = c(30, 25)
)

Accessing Elements

You can access elements using indices, names, or$ for named list/data frame elements.

  • Vectors and matrices use square brackets [] with numeric indices.
  • Lists and data frames can use $ for named elements/columns.
  • In matrices and data frames, [row, column] indexing is used; omit one to get a whole row or column.
  • Indexing in R starts at 1.
nums[1]        # First element of a vector
myList$name    # Access 'name' from a list
m[1, 2]        # Matrix element in row 1, col 2
df$age         # Column 'age' from data frame

Conversion Between Types

Use conversion functions to change between structures when needed.

as.list(nums)
as.vector(m)
as.data.frame(myList)

Best Practices

  • Choose the right structure based on data type and use case
  • Use str() to inspect structures
  • Prefer data frames for real-world datasets

Need Help?