R List
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A List is a collection of similar or different types of data.
In R, we use the list()
function to create a list. For example,
# list with similar type of data list1 <- list(24, 29, 32, 34) # list with different type of data list2 <- list("Ranjy", 38, TRUE)
Here,
- list1 - list of integers
- list2 - list containing string, integer, and boolean value
Access List Elements in R
In R, each element in a list is associated with a number. The number is known as a list index.
We can access elements of a list using the index number (1, 2, 3 …). For example,
list1 <- list(24, "Sabby", 5.4, "Nepal") # access 1st item print(list1[1]) # 24 # access 4th item print(list1[4]) # Nepal
In the above example, we have created a list named list1.
Here, we have used the vector index to access the vector elements
list1[1]
- access the first element 24list1[4]
- accesses the third element"Nepal"
Note: In R, the list index always starts with 1. Hence, the first element of a list is present at index 1, second element at index 2 and so on.
Modify a List Element in R
To change a list element, we can simply reassign a new value to the specific index. For example,
list1 <- list(24, "Sabby", 5.4, "Nepal") # change element at index 2 list1[2] <- "Cathy" # print updated list print(list1)
Output
[[1]] [1] 24 [[2]] [1] "Cathy" [[3]] [1] 5.4 [[4]] [1] "Nepal"
Here, we have reassigned a new value to index 2 to change the list element from "Sabby"
to "Cathy"
.
Add Items to R List
We use the append()
function to add an item at the end of the list. For example,
list1 <- list(24, "Sabby", 5.4, "Nepal") # using append() function append(list1, 3.14)
Output
[[1]] [1] 24 [[2]] [1] "Sabby" [[3]] [1] 5.4 [[4]] [1] "Nepal" [[5]] [1] 3.14
In the above example, we have created a list named list1. Notice the line,
append(list1, 3.14)
Here, append()
adds 3.14 at the end of the list.
Remove Items From a List in R
R allows us to remove items for a list. We first access elements using a list index and add negative sign -
to indicate we want to delete the item. For example,
[-1]
- removes 1st item[-2]
- removes 2nd item and so on.
list1 <- list(24, "Sabby", 5.4, "Nepal") # remove 4th item print(list1[-4]) # Nepal
Output
[[1]] [1] "Sabby" [[2]] [1] 5.4 [[3]] [1] "Nepal"
Here, list[-4]
removes the 4th item of list1.
Length of R List
In R, we can use the length()
function to find the number of elements present inside the list. For example,
list1 <- list(24, "Sabby", 5.4, "Nepal") # find total elements in list1 using length() cat("Total Elements:", length(list1))
Output
Total Elements: 4
Here, we have used the length()
function to find the length of list1. Since there are 4 elements in list1 so length()
returns 4.
Loop Over a List
In R, we can also loop through each element of the list using the for loop. For example,
items <- list(24, "Sabby", 5.4, "Nepal") # iterate through each elements of numbers for (item in items) { print(item) }
Output
[1] 24 [1] "Sabby" [1] 5.4 [1] "Nepal"
Check if Element Exists in R List
In R, we use the %in%
operator to check if the specified element is present in the list or not and returns a boolean value.
TRUE
- if specified element is present in the listFALSE
- if specified element is not present in the list
For example,
list1 <- list(24, "Sabby", 5.4, "Nepal") "Sabby" %in% list1 # TRUE "Kinsley" %in% list1 # FALSE
Output
TRUE FALSE
Here,
"Larry"
is present in list1, so the method returnsTRUE
"Kinsley"
is not present in list1, so the method returnsFALSE
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.