14 Feb R Lists
If you want to store data of different types, then use Lists in the R programming language. A list is ordered and can be changed. In this lesson, we will learn how to create a List and work on some List operations:
- Create a List
- Length of List
- Access List Items
- Find if a specific item is present in a List
- Change the value of a specific List item
Create a List
To create a List in the R programming language, use the list() function. Let us see an example to learn how to create a List:
1 2 3 4 5 6 7 |
# List of strings myList <- list("amit", "rohit", "virat", "tom", "john") # Display myList |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[[1]] [1] "amit" [[2]] [1] "rohit" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" |
Let us see another example to create a List with different datatypes:
1 2 3 4 5 6 7 |
# List with different datatypes myList <- list("amit", FALSE, 40, 45.24, c(5, 10, 15, 20, 25), "virat") # Display myList |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[[1]] [1] "amit" [[2]] [1] FALSE [[3]] [1] 40 [[4]] [1] 45.24 [[5]] [1] 5 10 15 20 25 [[6]] [1] "virat" |
Length of list
The length() method is used in the R programming language to get the length of a list. Let us see an example:
1 2 3 4 5 6 7 8 9 10 |
# List of strings myList <- list("amit", "rohit", "virat", "tom", "john") # Display myList # Get the list length length(myList) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[[1]] [1] "amit" [[2]] [1] "rohit" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" [1] 5 |
Access List Items
use the [] brackets in R to access the list items i.e. elements of a List. Just set the position number of the element you want to fetch. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 |
# List of strings myList <- list("amit", "rohit", "virat", "tom", "john") # Display myList # Access List items # Set the position of the item you want to access inside the [] brackets myList[3] |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[[1]] [1] "amit" [[2]] [1] "rohit" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" [[1]] [1] "virat" |
In the above example, we have accessed the 3rd item since we have set position 3.
Find if a specific item is present in a List
Use the %in% operator in the R programming language to find if an item i.e. element is in a List. If the item is present, TRUE is returned, else FALSE.
Let us see an example to find items in a List:
1 2 3 4 5 6 7 8 9 10 11 |
# List of strings myList <- list("amit", "rohit", "virat", "tom", "john") # Display myList # Find specific items in the List using the %n% operator "amit" %in% myList # Item is present "jacob" %in% myList # Item is not present |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[[1]] [1] "amit" [[2]] [1] "rohit" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" [1] TRUE [1] FALSE |
Change the value of a specific List item
A List can be changed after creating it. You can easily change any list item using the position of that specific item. In the below example, we will change the item at position 3. Let us see the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# List of strings myList <- list("amit", "rohit", "virat", "tom", "john") # Display the List myList print("Updated List...") # Change the list item at position 2 myList[2] <- "dhawan" # Updated List myList |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[[1]] [1] "amit" [[2]] [1] "rohit" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" [1] "Updated List..." [[1]] [1] "amit" [[2]] [1] "dhawan" [[3]] [1] "virat" [[4]] [1] "tom" [[5]] [1] "john" |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments