3 min read•august 9, 2024
Vectors are the backbone of data manipulation in R. They allow you to store and work with multiple values of the same type, making data analysis and calculations a breeze. From creating simple numeric lists to complex named vectors, R's versatile tools make operations intuitive and powerful.
Understanding how to create, access, and manipulate vectors is crucial for effective R programming. Whether you're performing arithmetic operations, logical comparisons, or advanced data transformations, mastering vector techniques will significantly enhance your ability to work with data in R.
[c()](https://www.fiveableKeyTerm:c())
function combines elements into a single vectornames()
function or during vector creationc(1, 2, 3, 4, 5)
or c(3.14, 2.718, 1.414)
c("apple", "banana", "cherry")
c(TRUE, FALSE, TRUE, TRUE)
c(age = 25, height = 180, weight = 70)
1:10
generates numbers from 1 to 10[seq()](https://www.fiveableKeyTerm:seq())
function for more complex sequences: seq(from = 0, to = 1, by = 0.1)
vector[3]
retrieves the third elementvector[-2]
returns all elements except the secondvector[2:5]
returns elements 2 through 5vector[c(1, 3, 5)]
returns first, third, and fifth elements[length()](https://www.fiveableKeyTerm:length())
function determines the number of elements in a vectorlength()
to iterate through vectors or check their sizec()
function or by assigning to a new index[sort()](https://www.fiveableKeyTerm:sort())
function, arranging elements in ascending or descending order+
), subtraction (-
), multiplication (*
), and division (/
) work element-wise&
, OR |
, NOT !
) apply to logical vectors<
, >
, ==
, !=
, <=
, >=
)seq()
function with various arguments (from, to, by, length.out)rep()
function, specifying values and number of repetitionsc()
function[paste()](https://www.fiveableKeyTerm:paste())
or [paste0()](https://www.fiveableKeyTerm:paste0())
to concatenate character vectors, adding separators if neededsapply()
or lapply()
union()
, intersect()
, setdiff()