Playback starts YouTube’s embedded player (privacy-enhanced mode).
R tutorial - How to Create and Name Vectors in R
ChannelDataCampDescription from the creator
Join DataCamp today, and start our interactive intro to R programming tutorial for free: 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!
About this tutorial
DataCamp published this 5:06 video tutorial on October 25, 2016. It has been watched 222,181 times on YouTube.
Share
Further reading
-
R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan
techvidvan.com
In this tutorial, we learned about R vectors. We learned how to create vectors and how many types of vectors there are. We also looked at coercion and the various indexing techniques for vectors. Finally, we explored th…
-
R Vector: Create, Modify and Access Vector Elements
datamentor.io
How to Create Vector in R? Vectors are generally created using the c() function. Since, a vector must have elements of the same type, this function will try and coerce elements to the same type, if they are different.
-
How to Create and Assign Named Vectors in R - dummies
dummies.com
How to Create and Assign Named Vectors in R By Andrie de Vries, Joris Meys You use the assignment operator ( <- ) to assign names to vectors in much the same way that you assign values to character vectors.
This tutorial in other languages
- FRtutoriel R - Comment créer et nom vecteurs R
- ESR tutorial - Cómo crear y Nombre de vectores en R
- PTR tutorial - Como criar e Nome vetores em R
- ITR Tutorial - Come creare e Nome vettori in R
- DER Tutorial - Erstellen und wie Name Vektoren in R
- NLR handleiding - Hoe te maken en Naam Vectoren in R
- RUR Учебник - Как создать и имя векторов в R
- PLTutorial R - Jak tworzyć i Nazwisko wektorów w R
- JARチュートリアル - どのようにRで作成し、名前ベクトルに
- ARR تعليمي - كيفية إنشاء واسم المتجهات في R
- HIआर ट्यूटोरियल - कैसे बनाने के लिए और अनुसंधान में नाम वेक्टर
- IDR tutorial - Cara Membuat dan Nama Vektor di R