In this introduction to R course you will learn how you can create and name your vectors in R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free-introduction-to-r
Hi again! In this video I’ll be talking about Vectors. A vector is nothing more than a sequence of data elements of the _same_ basic data type. Remember the atomic vector types I discussed before? You can have character vectors, numeric vectors, logical vectors, and many more.
First things first: creating a vector in R! You use the `c()` function for this, which allows you to combine values into a vector. Suppose you’re playing a basic card game, and record the suit of 5 cards you draw from a deck. A possible outcome and corresponding vector to contain this information could be this one
Of course we could also assign this character vector to a new variable, drawn_suits for example.
We now have a character vector, drawn_suits.
We can assert that it is a vector, by typing is dot vector drawn_suits
Likewise, you could create a vector of integers for example to store how much cards of each suit remain after you drew the 5 cards. Let’s call this vector remain. There are 11 more spades, 12 more hearts, 11 diamonds, and all 13 clubs still remain.
If you print remain to the console, …, it looks ok, but it’s not very informative. How does somebody else know that the first value corresponds to spades? Wouldn’t it be useful if you could attach labels to the vector elements? You can do this in R by naming the vector.
You can use the `names()` function for this. Let’s first create another character vector, `suits`, that contains the strings “spades”, “hearts”, “diamonds”, and “clubs”, the names you want to give your vector elements.
Now, this line of code, …, sets the names of the elements in `remain` to the strings in suits. If you now print remain to the console, … , you’ll see that the suits information is accompanied by the proper labels. Great! If you don’t want to bother with setting the names afterwards, you could just as well create a named vector with a one liner. You can use equals signs inside the `c()` function:
Notice that here, it’s not necessary to surround the names, “spades”, “hearts”, “diamonds” and “clubs”, with double quotes, although this also works perfectly fine.
In all three cases, the result is exactly the same. Under the hood, R vectors have attributes associated with them. What you did when you set the names of the remain vector, is actually setting the names attributes of the remains object. The `str()` function, that compactly displays the the structure of an R object, shows this.
You’ll have plenty of fun creating and naming variables in all sorts of ways, but before I let you to it, there are two more things I want to discuss with you.
First of all, remember the variables you’ve created in the previous chapter? These variables, such as `my_apples`, equal to 5, and `my_oranges`, equal to the character string “six” at some point, are actually all vectors themselves. R does not provide a data structure to hold a single number or a single character string or any other basic data type: they’re all just vectors of length 1. You can check this by typing is dot vector my_apples, which is TRUE, and is dot vector my_oranges, which is TRUE as well.
That these variables are actually vectors of length 1, can be checked using the length() function.
This contrasts with the other vectors we’ve created in this video: the drawn_suits vector, for example, has length 5.
The last important thing is that in R, a vector can only hold elements of the same type. They’re also often called *atomic vectors*, to differentiate them from *lists*, another data structure which can hold elements of different types. This means that you cannot have a vector that contains both logicals and numerics, for example. If you do try to build such a vector, R automatically performs coercion to make sure that you end up with a vector that contains elements of the same type. Let’s see how that works with an example.
In contrast to recording the suits you draw from a deck of cards, suppose now you’re recording the _ranks_ of the cards. You might want to combine the result of drawing 8 cards like this, creating a vector drawn_ranks.
If you now inspect this vector, you’ll see that the numeric vector elements have been coerced to characters, to end up with a homogeneous character vector.
This is also what the class function from before tells us.
The fact that R handles this for us automatically, ‘upgrading’ logicals to numerics and numerics to characters when necessary along the way, is useful but can also be dangerous, so be aware of this. If you want to store elements of different types in the same data structure, you’ll want to use a list. But that’s something for later.
Now, it’s time to step up your betting game in the interactive exercises!