If a vector has elements of different types, it is called a list in R programming.
A list is a flexible data structure that can hold elements of different types, such as numbers, characters, vectors, matrices, and even other lists.
How to create a list in R programming?
We can create a list using the list()
function.
x <- list("a" = 2.5, "b" = TRUE, "c" = 1:3)
x
Output
$a [1] 2.5 $b [1] TRUE $c [1] 1 2 3
Here, we create a list x
, of three components with data types double
, logical
and integer
vector respectively.
The structure of the above list can be examined with the str()
function
str(x)
Output
List of 3 $ a:num 2.5 $ b:logi TRUE $ c:int [1:3] 1 2 3
In this example, a
, b
and c
are called tags which makes it easier to reference the components of the list.
However, tags are optional. We can create the same list without the tags as follows. In such a scenario, numeric indices are used by default.
x <- list(2.5,TRUE,1:3)
x
Output
[[1]] [1] 2.5 [[2]] [1] TRUE [[3]] [1] 1 2 3
How to access components of a list?
Lists can be accessed in similar fashion to vectors. Integer, logical or character vectors can be used for indexing. Let us consider a list as follows.
x <- list(name = "John", age = 19, speaks = c("English", "French"))
# access elements by name
x$name
x$age
x$speaks
# access elements by integer index
x[c(1, 2)]
x[-2]
# access elements by logical index
x[c(TRUE, FALSE, FALSE)]
# access elements by character index
x[c("age", "speaks")]
Output
[1] "John" [1] 19 [1] "English" "French" $name [1] "John" $age [1] 19 $name [1] "John" $speaks [1] "English" "French" $name [1] "John" $age [1] 19 $speaks [1] "English" "French"
Indexing with [
as shown above will give us a sublist not the content inside the component. To retrieve the content, we need to use [[
.
However, this approach will allow us to access only a single component at a time.
x <- list(name = "John", age = 19, speaks = c("English", "French"))
# access element by name using single bracket []
x["age"]
# check the type of the result (single bracket returns a list)
typeof(x["age"])
# access element by name using double bracket [[]]
x[["age"]]
# check the type of the result (double bracket returns the content)
typeof(x[["age"]])
Output
$age [1] 19 [1] "list" [1] 19 [1] "double"
An alternative to [[
, which is used often while accessing content of a list is the $
operator. They are both the same, except that $
can do partial matching on tags.
x <- list(name = "John", age = 19, speaks = c("English", "French"))
# access element by exact matching using $
x$name
# access element by partial matching using $
x$age
# access element by partial matching using $
x$speaks
# create a list with similar tags
y <- list(n = "Alice", a = 25, s = c("Spanish", "Italian"))
# access element by partial matching using $
y$n
# access element by partial matching using $
y$a
# access element by partial matching using $
y$s
Output
[1] "John" [1] 19 [1] "English" "French" [1] "Alice" [1] 25 [1] "Spanish" "Italian"
How to modify a list in R?
We can change components of a list through reassignment. We can choose any of the component accessing techniques discussed above to modify it.
Notice below that modification causes reordering of components.
x <- list(name = "John", age = 19, speaks = c("English", "French"))
# access element by double brackets [[]] and update its value
x[["name"]] <- "Clair"
# print the updated list
x
Output
$name [1] "Clair" $age [1] 19 $speaks [1]"English" "French"
How to add components to a list?
Adding new components is easy. We simply assign values using new tags and it will pop into action.
x <- list(name = "Clair", age = 19, speaks = c("English", "French"))
# assign a new element to the list using double brackets [[]]
x[["married"]] <- FALSE
# print the updated list
x
Output
$name [1] "Clair" $age [1] 19 $speaks [1] "English" "French" $married [1] FALSE
How to delete components from a list?
We can delete a component by assigning NULL
to it.
x <- list(name = "Clair", age = 19, speaks = c("English", "French"))
# remove an element from the list using double brackets [[]]
x[["age"]] <- NULL
# print the structure of the updated list
str(x)
# remove an element from the list using $ notation
x$married <- NULL
# print the structure of the updated list
str(x)
Output
List of 2 $ name :chr "Clair" $ speaks:chr [1:2] "English" "French" List of 2 $ name :chr "Clair" $ speaks:chr [1:2] "English" "French"