R Tutorial - How to Subset & Extend Lists in R

  Рет қаралды 47,061

DataCamp

DataCamp

8 жыл бұрын

Discover how to subset and extend lists in R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: www.datacamp.com/courses/free...
Creating a list is not so different from creating a vector, is it? Unfortunately, subsetting lists and subsetting vectors are not that similar. Let's retake the music artist example. Remember the list `song` that contained both information on a song, as well as on a similar song?
You might have noticed that the printout of the `song` list is not what it should be. I actually included the output of calling `str(song)` here instead, which is more compact and readable. I'll also do this in the list printouts that follow. Anyways, you see that the list contains 4 elements, a character, two numerics and another list.
Suppose now you want to extract the song title, "Rsome times", from this list. When you were working with vectors and matrices, this was dead simple. You simply put the index of the song title, which is 1, in square brackets and you get the elements. Well, let's try it out!
That's not what we want! This is not a character string. It's a list, containing only the element that corresponds to the title information. This is a very important detail when trying to subset lists. If you use single brackets on lists, you subset the list, but also a list gets returned. If you want to select the actual title from `song`, so the character string Rsome times, you'll need double square brackets instead of single ones:
That looks more like it! The difference between single brackets and double brackets is not big on your keyboard, but it sure is important from an R perspective. Just remember this: subsetting lists using single brackets results in lists, while you can only access a single element using double brackets. This difference doesn't limit you from supercharging your subsetting operations of course. Suppose for example you want to select both the title and the track from `song`. You can use a vector with the indices 1 and 3 inside single brackets to subset the list:
We end up with a list of length 2 this time, only containing the title and the track. Could this also work with double brackets? Let's try it out.
It generates an index out of bounds errror. That's pretty strange? Well, not really. It's because the double brackets are only to select single elements from a list. In fact, this command is actually equivalent to this one:
This command means: take the first element from the song list, and from that list, take the third element. But the first element from song is simply a character vector of length 1, so there's no way of selecting the third element from it. Nonetheless, you use this approach to select for example the title of the similar song, whose information is stored in the fourth element of `song`. In this case you first want to take the fourth element, and then the first element:
This indeed is the title we were looking for! This could also be coded as follows:
What about subsetting by name and by logicals, I hear you asking? Well, subsetting by name is super straightforward. To select the second element from song, for example, you can just as well use the string "duration" inside double brackets:
This subsetting by names of course also works with single brackets, both to build a one-element list as well as to select multiple elements:
Subsetting by logicals is only possible for the single-bracket version. To select the second and third element from song, this works, ..., but this doesn't.
That's because the second line is interpreted as follows:
and this makes no sense whatsoever.
Another way that is totally new here, is the use of the dollar sign to select an element from a list. It works just the same as the double brackets but only works on named lists. If you want to select the duration string from `song` for example, you can use song, dollar sign, duration:
This seamlessly brings me to adding elements to your list. Before you want to release your new song online, suppose you first want to have some of your friends to check it out. You decide to add a vector of friends names to the song list to remember which ones you've sent it to. Let's first create a new vector `friends`.
To add this information to the list `song` under the name `sent`, you have diferent options. Why not start with the dollar notation first? You simply type song, dollar sign, sent and then assign friends to it:
If you now have a look at song again, you'll see that there is a fifth list element in song now. The same result could have been reached using the double square brackets:
It's even possible to add elements to embedded lists.

Пікірлер: 6
@RobinBeaumont
@RobinBeaumont 5 жыл бұрын
Very clear and detailed. Good that you cover the difference between double and single square brackets in lists and also how to access specific sub elements like a value in a matrix or a element of a list within a list
@patrickluong2496
@patrickluong2496 Жыл бұрын
Was watching this on the elliptical and then did dataquest afterwards with conflicting info. I note that this video was created 7 years ago so syntax in R might have been updated, but I see that in this video around 2:36, "song[[c(1,3)]]" is not valid syntax but today it allows you to select the first object and third element. Wanted to relay this.
@Rioldel
@Rioldel 4 жыл бұрын
you guys make it so easy :))
@gokulsreekumar4371
@gokulsreekumar4371 5 жыл бұрын
THANK YOU VERY MUCH, REALLY HEPLFUL
@AMITAS5
@AMITAS5 6 жыл бұрын
Very nice videos
@santiagorugeles4062
@santiagorugeles4062 3 жыл бұрын
Super useful.
R Tutorial - Using the Data Frame in R
5:24
DataCamp
Рет қаралды 219 М.
How to Create and Use Vectors in R
15:06
Becoming a Data Scientist
Рет қаралды 11 М.
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 2 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 94 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 445 М.
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 20 МЛН
R tutorial - Subsetting your Vectors in R
5:32
DataCamp
Рет қаралды 89 М.
How to Subset (Navigate) and Add to Lists in R
8:30
Becoming a Data Scientist
Рет қаралды 1,6 М.
R tutorial - How to Create & Name Lists in R
3:55
DataCamp
Рет қаралды 75 М.
Why you need to use lists more in R
10:24
Andres Quintero
Рет қаралды 1,2 М.
Working with lists in R
7:29
Equitable Equations
Рет қаралды 7 М.
Transform Your Data Like a Pro with {tidyr} and Say Goodbye to Messy Data!
13:17
R tutorial - Using Factors in R
5:40
DataCamp
Рет қаралды 207 М.
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 2 МЛН