We have covered quite a lot in R in the last few posts. Let's do a small Recap shall we.
Vector : 1 Dimensional data structure which can only hold a single data type
Matrix : 2 Dimensional data structure which can only hold a single data type. ( Remember the Matrix Revolution movie 💪 )
Factor : a categorical variable can only hold a limited number of categories. Eg: Blood types
# Numeric vector: 1 up to 10
geek_vector <- 1:10
# Numeric matrix: 1 up to 9
geek_matrix <- matrix(1:9, ncol = 3)
# Factor of sizes
geek_factor <- factor(c("M","S","L","L","M"), ordered = TRUE, levels = c("S","M","L"))
# Construct my_list with these different elements
geek_list <- list(geek_vector,geek_matrix,geek_factor)
You can define names by using the names() function.
names(geek_list) <- c("vec","mat","fac")
You can name the elements at the time of creating the list as well.
# Create actors and ratings
actors_vector <- c("Jack Nicholson","Shelley Duvall","Danny Lloyd","Scatman Crothers","Barry Nelson")
ratings_factor <- factor(c("Good", "OK", "Good", "Perfect", "Bad", "Perfect", "Good"),
ordered = TRUE, levels = c("Bad", "OK", "Good", "Perfect"))
# Create shining_list
shining_list <- list(title = "The Shining" ,actors = actors_vector , ratings= rating_factor )
Subset and extend lists
Sub settling list is not similar to sub setting vectors.
As you can see the list contains 4 elements. a Character , 2 numeric's and a list
Ok lets try song[1]
> song[1]
$title
[1] "Rsome times"
Now let's try song[[1]]
> song[[1]]
[1] "Rsome times"
When you use single square brackets it will return a list it self. If you use double square brackets it will return the element.
Lets say you want to get the title and the track as a list. You can use the below code to get that.> song[c(1,3)]
$title
[1] "Rsome times"
$track
[1] 5
So what if we use double brackets here ??? Lets try that out
> song[[c(1,3)]]
Error in song[[c(1, 3)]] : subscript out of bounds
As you can see it generates an index out of bounds error. It looks strange isn't it . Well actually you can only use double brackets to get single elements. When you use double brackets in the about code it actually represents
song[[c(1,3)]] == song[[1]][[3]]
It means take the 1st element of the song list and from that take the 3rd element. Since the first element is a character vector with length 1 and there are no more elements in that.
But this will work with the 4th element which is a list. Let's try that out.
> song[[4]][[1]]
[1] "R you on time?"
It returns the title of the list.
Sub set by name
It is pretty straight forward. You can use the name to select the element you require.
Eg: song[["duration"]]
> song[["duration"]]
[1] 190
> song["duration"]
$duration
[1] 190
In this case both single and double brackets works the same way.
You could also use multiple elements as well.
> song[c("duration","similar")]
$duration
[1] 190
$similar
$similar$title
[1] "R you on time?"
$similar$duration
[1] 230
Sub set Logicals
Sub setting by Logicals will only work with single brackets.
> song[c(FALSE,TRUE,TRUE,FALSE)]
$duration
[1] 190
$track
[1] 5
Let's see what happens when we use double brackets.
> song[[c(FALSE,TRUE,TRUE,FALSE)]]
Error in song[[c(FALSE, TRUE, TRUE, FALSE)]] :
attempt to select less than one element in integerOneIndex
Returns as error. When we use double brackets it actually interprets like below
song[[F]][[T]][[T]][[F]]
Well it doesn't make any sense right!!!!!!!
$ and extending
Another way to select elements in a list is to use the $ sign
> song$duration
[1] 190
You can also assign values using the $ sign.
Lets create a friends vector
> friends <- c("Kurt","Steve","John","Jane")
Now lets assign it to a new element named Sent.
> song$sent <- friends // this can be also done by using --- > song[["sent"]] <- friends
Now let's look at our list
> song
$title
[1] "Rsome times"
$duration
[1] 190
$track
[1] 5
$similar
$similar$title
[1] "R you on time?"
$similar$duration
[1] 230
$sent
[1] "Kurt" "Steve" "John" "Jane"
Note : use of the str function provided more efficient details 💃
> str(song)
List of 5
$ title : chr "Rsome times"
$ duration: num 190
$ track : num 5
$ similar :List of 2
..$ title : chr "R you on time?"
..$ duration: num 230
$ sent : chr [1:4] "Kurt" "Steve" "John" "Jane"
So to access the title element in the similar list of the song list you can do something like this.
> song$similar$title
[1] "R you on time?"
Wrap up
- [[ or [ ?
- [[ to select a single element
- [ to select a sub list
- [[ and $
- Both of these can be used to sub set as well as add elements to the list