List in R

The list in R contains several unique data types inside it. A collection of data that is ordered and editable is called a list in R.

We used the list() function to create a list:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# List of strings
l <- list("Python", "R", "Java")
# Print the list
l
# List of strings l <- list("Python", "R", "Java") # Print the list l
# List of strings
l <- list("Python", "R", "Java")

# Print the list
l

Output

List in R

Access List in R

To access the list items, refer to its index number inside brackets. The first item has index 1, the second item has index 2, and so on:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
l[1]
l <- list("Python", "R", "Java") l[1]
l <- list("Python", "R", "Java")

l[1]

Output

1 8

Change Item Value

Refer to the index number to change the value of a specific item:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
l[1] <- "C++"
# Print the updated list
l
l <- list("Python", "R", "Java") l[1] <- "C++" # Print the updated list l
l <- list("Python", "R", "Java")
l[1] <- "C++"

# Print the updated list
l

Output

1 9

List Length

We can use the length() function to find out how many items a list has:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
length(l)
l <- list("Python", "R", "Java") length(l)
l <- list("Python", "R", "Java")

length(l)

Output

1 10

Check if Item Exists

We can use the %in% operator to find out if a specified item is present in a list:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Check if "Python" is present in the list:
l <- list("Python", "R", "Java")
"Python" %in% l
# Check if "Python" is present in the list: l <- list("Python", "R", "Java") "Python" %in% l
# Check if "Python" is present in the list:
l <- list("Python", "R", "Java")

"Python" %in% l

Output

1 11

Add List Items

We can use the append() function to add an item to the end of the list:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Add C++ in the list
l <- list("Python", "R", "Java")
append(l, "C++")
# Add C++ in the list l <- list("Python", "R", "Java") append(l, "C++")
# Add C++ in the list
l <- list("Python", "R", "Java")

append(l, "C++")

Output

1 12

We can add “after=index number” in the append() function to add an item to the right of a specified index:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
append(l, "C++", after = 2)
l <- list("Python", "R", "Java") append(l, "C++", after = 2)
l <- list("Python", "R", "Java")

append(l, "C++", after = 2)

Output

1 13

Remove List Items

List items can be removed from the list. Mentioned below example created a newly updated list without the Python” item:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
nl <- l[-1]
# Print the new list
nl
l <- list("Python", "R", "Java") nl <- l[-1] # Print the new list nl
l <- list("Python", "R", "Java")

nl <- l[-1]

# Print the new list
nl

Output

1 14

Indexes range

The range of indexes can be specified where to begin and where to close the range, by using the operator ( : ).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java", "C++", "Ruby", "HTML")
(l)[2:4]
l <- list("Python", "R", "Java", "C++", "Ruby", "HTML") (l)[2:4]
l <- list("Python", "R", "Java", "C++", "Ruby", "HTML")

(l)[2:4]

Output

1 15

Loop Through a List

To loop through the list items we can use a for loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l <- list("Python", "R", "Java")
for (x in l) {
print(x)
}
l <- list("Python", "R", "Java") for (x in l) { print(x) }
l <- list("Python", "R", "Java")

for (x in l) {
  print(x)
}

Output

1 16

Join Two Lists

In R programming, there are many ways to concatenate two or more lists. To combine two elements together the c() function is used commonly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l1 <- list("Python", "R", "Java")
l2 <- list(1,2,3)
l3 <- c(l1,l2)
l3
l1 <- list("Python", "R", "Java") l2 <- list(1,2,3) l3 <- c(l1,l2) l3
l1 <- list("Python", "R", "Java")
l2 <- list(1,2,3)
l3 <- c(l1,l2)

l3

Output

1 17

Merging Lists

One or more lists can be merged into one list in R programming. The list() function helps in merging. To merge the list you have to pass all lists in function as a parameter, then it returns a list that contains all the elements which are present in the lists. Check the mentioned merging process is done.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
el <- list(2,4,6,8,10)
ol <- list(1,3,5,7,9)
merged.list <- list(el,ol)
print(merged.list)
el <- list(2,4,6,8,10) ol <- list(1,3,5,7,9) merged.list <- list(el,ol) print(merged.list)
el <- list(2,4,6,8,10)  
ol <- list(1,3,5,7,9)  
    
merged.list <- list(el,ol)  
   
print(merged.list)  

Output:

list12