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.”
1 in R, not 0 like many other languages.# Numeric vector
nums <- c(1, 2, 3)
# Character vector
words <- c("apple", "banana")Lists are versatile containers that can hold elements of different types(numeric, character, logical, vectors, other lists, and more).
$ syntax.[[ ]] or the $ operator for named items.myList <- list(name = "Alice", age = 30, scores = c(85, 90, 95))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.
matrix(), specifying nrow and/or ncol.byrow = TRUE to fill row-wise.[row, column].m <- matrix(1:6, nrow = 2, ncol = 3)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.
str(df) to see the structure, and summary(df) for summary statistics.df <- data.frame(
name = c("Alice", "Bob"),
age = c(30, 25)
)You can access elements using indices, names, or$ for named list/data frame elements.
[] with numeric indices.$ for named elements/columns.[row, column] indexing is used; omit one to get a whole row or column.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 frameUse conversion functions to change between structures when needed.
as.list(nums)
as.vector(m)
as.data.frame(myList)str() to inspect structures