Creating Lists: Understanding Subsetting and Vector Operations
Creating lists is not so different from creating vectors, unfortunately. However, subsetting lists and subsetting vectors are not that similar. To understand this concept better, let's take 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. In reality, I actually included the output of calling `SDR` song here instead, which is more compact and readable. From now on, we will also include this in our list printouts.
As you see, the list contains four elements: a character string and another list. Now, you want to extract the song title or sometimes from this list when working with vectors and matrices, it was that simple. You simply put the index of the song title which is one and square brackets, and you got the element. Well, let's try it out. That's what we want. This is not a character string; it's a list containing only one 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 will subset the list but also get a list as a result. You can then select the actual title from song by using double square brackets instead of single ones. This looks more like it's the difference between single brackets and double brackets is not bigger in your keyboard but it sure is important from an art perspective.
Just remember, this means that subsetting lists using single brackets results in a list while you can only access a single element using double brackets. This difference doesn't limit you from supercharging your subset operations. For example, suppose you want to select both the title and track from song. You can use a vector with the indices one and three inside single brackets to subset the list. We end up with a list of length two this time only containing the title and a track.
However, we need to try it out to see if double brackets work as well. When we do, it generates an index out of bounds error that's pretty strange but not really. It's because double brackets are only used to select single elements from a list. In fact, this command is actually equivalent to the following one. This command means fake the first element from the song list and from that list fake the third element. But the first element from song is simply a character vector of length one so there's no way of selecting the third element from it.
Nonetheless, we can use this approach to select for example the title of a similar song whose information is stored in a fourth element of song. In this case, you first want to take the fourth element and then the first element. This is indeed that idle we were looking for. This could also be coded as follows.
Now let's talk about subsetting `myName` and `biologicals`. When you hear me asking how to subset my 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. Subsetting my names of course also works with single brackets both to build a one-element list and to select multiple elements. Subsetting `biologicals` is only possible for the single bracket version to select a second and third element from song. It works but it 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 as well as double brackets but only works on named lists if you want to select the duration string from song. For example, you can use `song$duration` this seamlessly brings us to adding elements to our list before we want to release our 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 `sense`, you have different options why not start with a dollar notation first? You simply type some dollar sign `cents` 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 double square brackets. It's even possible to add elements to embedded lists to add a reason why you decided to remove the similar song from your album. You could write something like this that's all we need to know about lists for now. There are two main takeaways here:
First, you use double square brackets to select the list elements while single brackets result in the sub-list containing the specified element.
Second, you can use a double square brackets and its dollar sign equivalent both to subset and add elements to your list up to exercises now.
"WEBVTTKind: captionsLanguage: encreating 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 SDR 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 four elements a character to numerix and another list suppose now you want to extract the song title or sometimes from this list when you were working with vectors and matrices this was that simple you simply put the index of the song title which is one and square brackets and you got the elements well let's try it out that's what we want this is not a character string it's a list containing only one element that corresponds to the title information this is a very important detail or 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 are sometimes you'll need double square brackets instead of single ones that looks more like it's the difference between single brackets and double brackets is not bigger in your keyboard but it sure is important from an art perspective just remember this sub setting lists using single brackets results in lists while you can only access a single elements using double brackets this difference doesn't limit you from supercharging your sub setting operations of course suppose for example you want to select both the title and track from song you can use a vector with the indices one and three inside single brackets to subset the list we end up with a list of length two this time only containing the title and a track could this also work with double brackets let's try it out it generates an index out of bounds error that's pretty stranger 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 fake the first element from the song list and from that list fake the third element but the first element from song is simply a character vector of length one so there's no way of selecting the third element from it nonetheless you can use this approach to select for example the title of a similar song whose information is stored in a fourth element of song in this case you first want to take the fourth element and then the first element this is indeed that the idle we were looking for this could also be coded as follows so what about subsetting my name and biologicals I hear you asking well subsetting my 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 sub setting my names of course also works with single brackets both to build a one element list as well as to select multiple elements subsetting biologicals is only possible for the single bracket version to select a second a third element from the 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 sense you have different options why not start with a dollar notation first you simply type some dollar sign cents 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 to add a reason why you decided to remove the similar song from your album you could write something like this that's all you need to know about lists for now there are two main takeaways here first you use double square brackets to select the list elements while single square brackets results in the sub list containing the specified elements second you can use a double square brackets and it's dollar sign equivalent both two subsets as well as to add elements to your list up to the exercises now chop-chopcreating 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 SDR 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 four elements a character to numerix and another list suppose now you want to extract the song title or sometimes from this list when you were working with vectors and matrices this was that simple you simply put the index of the song title which is one and square brackets and you got the elements well let's try it out that's what we want this is not a character string it's a list containing only one element that corresponds to the title information this is a very important detail or 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 are sometimes you'll need double square brackets instead of single ones that looks more like it's the difference between single brackets and double brackets is not bigger in your keyboard but it sure is important from an art perspective just remember this sub setting lists using single brackets results in lists while you can only access a single elements using double brackets this difference doesn't limit you from supercharging your sub setting operations of course suppose for example you want to select both the title and track from song you can use a vector with the indices one and three inside single brackets to subset the list we end up with a list of length two this time only containing the title and a track could this also work with double brackets let's try it out it generates an index out of bounds error that's pretty stranger 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 fake the first element from the song list and from that list fake the third element but the first element from song is simply a character vector of length one so there's no way of selecting the third element from it nonetheless you can use this approach to select for example the title of a similar song whose information is stored in a fourth element of song in this case you first want to take the fourth element and then the first element this is indeed that the idle we were looking for this could also be coded as follows so what about subsetting my name and biologicals I hear you asking well subsetting my 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 sub setting my names of course also works with single brackets both to build a one element list as well as to select multiple elements subsetting biologicals is only possible for the single bracket version to select a second a third element from the 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 sense you have different options why not start with a dollar notation first you simply type some dollar sign cents 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 to add a reason why you decided to remove the similar song from your album you could write something like this that's all you need to know about lists for now there are two main takeaways here first you use double square brackets to select the list elements while single square brackets results in the sub list containing the specified elements second you can use a double square brackets and it's dollar sign equivalent both two subsets as well as to add elements to your list up to the exercises now chop-chop\n"