6.3 More Complicated Data
But what’s that I hear you saying? “Dr. White, can’t I use anything other than simple numbers? What about if I want to store a bunch of numbers at once? Do I have to save each one as a its own variable? You said in the first class that computers can do billions of calculations per second! I can’t type in a billion variables - I have to get to my Latin class!”
Never fear - there are lots of ways to store data in R. For example, you can store multiple numbers in a data type called a vector:
> v <- c(1, 2, 3, 4)
This should pop up in the Environment pane of RStudio.
See what we did there? We used the function c(...)
and put all the data that we wanted in the vector inside the braces (separated by commas). (More on functions soon).
Here’s a question for you: can you use the operators from the first section on vectors?
For example, what happens if you do:
> v2 <- c(5, 6, 7, 8)
> v + v2
What about the other operators? Do they all work on vectors?
What happens if the vectors are different lengths? For example:
> v + c(9, 10, 11)
Can you apply an operator to a vector and a number? Does it always give you the result that you expect? For example:
> v + 1