Vectors Flashcards
What is a vector?
One dimensional array
Can a single vector hold elements of multiple data types?
No
What is the function to create a vector?
c()
Ex: c(“a”, “b”, “c”)
What is the function to name elements of a vector?
names()
Ex: names(my_vector) <- c(“name1”, “name2”)
What happens when two vectors are added, subtracted, etc.?
Ex: c(1, 2, 3) + c(4, 5, 6)
The result is calculated element-wise.
Ex Result: 5, 7, 9 is printed
Which function sums all elements in a vector?
sum()
Ex: sum(my_vector)
What index do vectors in R start at?
1
How can you select a single element from a vector?
[num]
Ex: my_vector[2] OR my_vector[“Monday”]
How can you select multiple out-of-sequence elements from a vector?
[c()]
Ex: my_vector[c(1, 5)] OR my_vector[c(“Monday”, “Wednesday”)] OR my_vector[c(TRUE, FALSE, TRUE)]
How can you select multiple in-sequence elements from a vector?
[num1:num2]
Ex: my_vector[2:5]
What happens when a vector is compared to a single object?
Ex: c(1, 3, 5) > 4
Each element in the vector is compared to the object.
Ex Result: FALSE FALSE TRUE is printed