4 min read•august 14, 2024
Vectors and matrices are fundamental data structures in R, forming the building blocks for more complex data manipulation. They allow you to store and organize data efficiently, whether it's simple numeric values or complex multidimensional arrays.
Understanding how to create, manipulate, and perform operations on vectors and matrices is crucial. These skills set the foundation for working with larger datasets, performing statistical analyses, and creating visualizations in R. Mastering these concepts opens doors to more advanced data handling techniques.
[c()](https://www.fiveableKeyTerm:c())
function, which combines arguments into a
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("a", "b", "c", "d")
[ ]
to select specific elements
numeric_vector[1]
returns the first elementnumeric_vector[-3]
returns the vector without the third elementnames(numeric_vector) <- c("one", "two", "three", "four", "five")
###[matrix](https://www.fiveableKeyTerm:Matrix)()_0###
function, specifying the data, number of rows, and number of columns
matrix(1:6, nrow = 2, ncol = 3)
[cbind()](https://www.fiveableKeyTerm:cbind())
or [rbind()](https://www.fiveableKeyTerm:rbind())
to create matrices by columns or rows
cbind(1:3, 4:6)
creates a matrix with two columns[ ]
to select specific elements, rows, or columns
matrix[1, 2]
returns the element in the first row and second columnrownames(matrix) <- c("row1", "row2")
<
, >
, <=
, >=
, ==
, !=
) to compare values in atomic vectors, returning logical vectors
numeric_vector > 3
returns a logical vector indicating which elements are greater than 3factor()
factor(c("a", "b", "a", "c"))
creates a factor with three levelslist()
function to combine elements of different data types
list(1, "a", TRUE)
creates a list with numeric, character, and logical elementsdata.frame(x = 1:3, y = c("a", "b", "c"))
creates a data frame with two columns+
, -
, *
, /
, ^
) for element-wise operations on vectors
numeric_vector + 1
adds 1 to each element of the vector*
for element-wise multiplication and %*%
for
matrix1 * matrix2
performs element-wise multiplicationsum()
, [mean()](https://www.fiveableKeyTerm:mean())
, min()
, max()
, and prod()
functions
sum(numeric_vector)
calculates the sum of all elements in the vectorrowSums()
, rowMeans()
, colSums()
, and colMeans()
functions
rowSums(matrix)
computes the sum of each row in the matrixNA
) have special behavior in mathematical operationsNA
will return NA
as the resultna.rm
argument set to TRUE
to remove missing values from calculations
mean(numeric_vector, na.rm = TRUE)
calculates the mean excluding missing valuesapply()
function to apply a function to the rows or columns of a matrix
apply(matrix, 1, sum)
applies the sum()
function to each row of the matrix1
for rows or 2
for columns as the second argument in apply()
lapply()
, returning a list of the same length as the input
lapply(list, sqrt)
applies the square root function to each element of the listlapply()
to a vector or matrix using sapply()
when possible
sapply(numeric_vector, sqrt)
applies the square root function and returns a tapply()
to apply a function to subsets of a vector based on a grouping factor
tapply(vector, group_factor, mean)
calculates the mean for each group defined by the factormapply()
mapply(sum, list1, list2)
applies the sum()
function to the corresponding elements of list1
and list2