R tutorial - How to Create and Name Vectors in R

preview_player
Показать описание
In this introduction to R course you will learn how you can create and name your vectors in 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!
Рекомендации по теме
Комментарии
Автор

Thank you😭 TA very unhelpful, I’ve been sitting here trying not to cry after trying and failing for hours but your video helped me in less than 5 minutes💜💜
- thank you, very grateful!

cassm
Автор

Men, I loved the way you explained everything, very calmed and making everything look simple. Thanks for this awesome explanation

mentefria
Автор

I'm taking this on the website and app. I was getting a little lost but these videos have been very helpful. Thank you.

ChristyD_RN
Автор

This video answers my biggest question!!! Thanks man, you are awesome

zhaofour
Автор

excellent work and the teaching is so awesome, anyone can understand and It covers from low level to high level .looking forward for more videos on other languages

snehithreddygankidi
Автор

Thank you :) please continue making videos it was truly helpful.

sundusy
Автор

There is small mistake at 1:52 you said to punch in suits but the demo prints remain. It has caused me a little confusion.

sumitpoojary
Автор

Is there a way to create a vector containing the values of a given column of a datafile ?

marwannaciri
Автор

Why are there double quotation mark for each character element but not for numeric? I am kinda confused. Thanks !

Itthew
Автор

at 2:40 what does the -attr(*, "names") doing? I am googling without any luck. thanks in advance!

m.d.
Автор

I DO NOT HAVE ANY KNOWLAGDE ABOUT PROGRAMMING. BUT FOR MY MASTERS STUDIES R PROGRAMMING IS MANDATORY. ANYONE CAN GUIDE ME FROM WHERE I HAVE TO START LEARNING?

fouziasubhani
Автор

though this was a lesson for math G12 dint expect it to be about Coding Bruh

somethingrandomidontknow