In this tutorial you will learn about the R Lists and its application with practical example.
R Lists
List is a basic data structure that can contain elements of different types. It is similar to vectors except it can contains mixed data i.e. numbers, strings, vectors and another list inside it.
Example:-
1 |
list(1.5, TRUE, c(2,4,6)) |
Creating a List
A list can be created using list() function as following –
Example:-
Following is an example to create a list containing strings, numbers, vectors and a logical values.
1 2 3 4 |
print("W3Adda - R Creating List") x <- list("W3Adda", 1.5, TRUE, c(2,4,6)) print(x) typeof(x) |
Output:-
Naming List Elements
In R, we can assign name or tags to the list elements, that makes it easy to reference the individual element of the list.
Example:-
Following is an example to create same list with the tags as follows.
1 |
x <- list("a" = 1.5, "b" = TRUE, "c" = c(2,4,6)) |
Here,
a, b and c are tags which makes it easier to reference the respective list element.
Accessing List Elements
List elements can be accessed in same as of vectors. An index value can be logical, integer or character.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
print("W3Adda - R Accessing List Elements") # Accessing list elements using integer indexing. t <- list("a" = 1.5, "b" = TRUE, "c" = c(2,4,6)) u <- t[c(1,3)] print("Accessing list elements using integer indexing.") print(u) # Accessing list elements using logical indexing. v <- t[c(FALSE,TRUE,FALSE)] print("Accessing list elements using logical indexing.") print(v) # Accessing list elements using negative indexing. x <- t[c(-2)] print("Accessing list elements using negative indexing.") print(x) # Accessing list elements using character indexing. b <- t[c("a","b")] print("Accessing list elements using character indexing.") print(b) |
Output:-
Add Element To List
An element can be simply added by assigning a values with new tags.
Example:-
1 2 3 4 |
print("W3Adda - R Add List Element") x <- list("a" = 1.5, "b" = TRUE, "c" = c(2,4,6)) x['d'] <- "w3adda" print(x) |
Here, we have added a new list element with “d” tag and assigning it value “w3adda”. When we run the above R script, we will see following output –
Output:-
Update List Element
A list element can be modified by accessing element and assigning it new value.
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Add Update Element") x <- list("a" = 1.5, "b" = TRUE, "c" = c(2,4,6)) print("List Element Before Update") print(x[1]) print("List Element After Update") x[1] <- 2.5 print(x[1]) |
Here, we have updated list element with index position 1 with new value. When we run the above R script, we will see following output –
Output:-
Delete Element From List
An element can be simply deleted by assigning NULL value to it.
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Delete List Element") x <- list("a" = 1.5, "b" = TRUE, "c" = c(2,4,6)) print("List Element Before Delete") print(x) print("List Element After Delete") x[1] <- NULL print(x) |
Here, we have deleted list element with index position 1 by assigning it NULL. When we run the above R script, we will see following output –
Output:-
Merging Lists
In R, we can create a new list by merging multiple list using list() function as following –
Example:-
1 2 3 4 5 6 7 |
print("W3Adda - R Merge List") list1 <- list(1,2,3) list2 <- list("a","b","c") # Merge the two lists. list3 <- c(list1,list2) # Print the merged list. print(list3) |
Here, we have two list list1 and list2, and we have merge both of the list into list3. When we run the above R script, we will see following output –
Output:-
Converting List to Vector
The unlist() function can be used to convert a list object into a vector.
Example:-
1 2 3 4 5 |
print("W3Adda - R Conver List to Vector") list1 <- list(1,2,3) print(list1) list2 <- unlist(list1) print(list2) |
Output:-