top of page

List in R Programming

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("pankaj", 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, "pankaj", 5.4, "US")

# access 1st item
print(list1[1]) # 24

# access 4th item
print(list1[4]) # US

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 24

  • list1[4] - accesses the third element "us"

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, "pankaj", 5.4, "p4n")

# 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] "p4n"

Here, we have reassigned a new value to index 2 to change the list element from "pankaj" 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, "pankaj", 5.4, "p4n")

# using append() function 
append(list1, 3.14)

Output

[[1]]
[1] 24

[[2]]
[1] "pankaj"

[[3]]
[1] 5.4

[[4]]
[1] "p4n"

[[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, "pankaj", 5.4, p4n")

# remove 4th item
print(list1[-4]) # p4n

Output

[[1]]
[1] "pankaj"

[[2]]
[1] 5.4

[[3]]
[1] "p4n"

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, "pankaj", 5.4, "p4n")

# 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

items <- list(24, "pankaj", 5.4, "p4n")

# iterate through each elements of numbers
for (item in items) {
    print(item)
}

Output

[1] 24
[1] "pankaj"
[1] 5.4
[1] "p4n"

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 list

  • FALSE - if specified element is not present in the list

For example,


list1 <- list(24, "pankaj", 5.4, "india")

"pankaj" %in% list1 # TRUE

"p4n" %in% list1 # FALSE

Output

TRUE
FALSE

Here,

  • "pankaj" is present in list1, so the method returns TRUE

  • "p4n" is not present in list1, so the method returns FALSE





Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Yorumlar


bottom of page