Remove Element from List in R (7 Example Codes) | How to Delete a List Component
In this tutorial, I will show you how to remove one or multiple elements from a list in R. The article is structured as follows:
- Creating an Example List in R
- Removing One Specific List Element (Examples 1, 2, 3 & 4)
- Deleting Several List Elements at Once (3 Examples)
- Further Resources on the Handling of Lists
Let’s move directly to the R syntax…
Create Example List
Before we can start with the examples, we need to create an example list in R:
my_list <- list(a = c(1, 4, 2, 7), # Create example list b = 555, c = "hello")
Figure 1: Example List in R.
Our example list contains three list elements, which are named a, b, and c.
In the following, I will show you four examples how to remove a certain element from this list…
Example 1: Remove Element from List with minus sign
In the first example, we will delete the second list component with the minus sign:
my_list[- 2] # Remove list element with -
Figure 2: Example List After Removing List Element.
As you can see based on Figure 2, we just removed the second list element of our example list.
In order to delete this list component, we just needed to write a square bracket, a minus sign, and the positioning of the list element we wanted to delete (i.e. [- 2]) behind the name of our list.
However, R provides many ways for the deletion of list elements and depending on your specific situation, you might prefer one of the other solutions. So let’s move on to the next example…
Example 2: Remove Element from List with NULL Assignment
Another way for deleting list elements if the assignment of NULL to the list elements we want to delete:
my_list_2 <- my_list # Replicate list my_list_2 <- NULL # Remove list elements with NULL my_list_2 # Print updated list to console
Note: It might be preferable to create a replication of our list before applying this method (as we did with my_list_2), since the original list object is replaced otherwise.
Example 3: Remove List Element by Name with %in% Operator
The %in% operator returns a logical vector, which indicates whether a certain value of a data object exists in another element.
In our specific example, we are checking at which position the names of our list are not equal to b. As in Example 1, we are then subsetting our list with square brackets.
Let’s do this in practice:
my_list[names(my_list) %in% "b" == FALSE] # Remove list elements %in%
As you can see after running this R code, we again deleted the second list component from our example list.
Example 4: Remove List Element by Name with !=
This example is similar to Example 3. We are checking again, at which position the names of our list are unequal to b:
my_list[names(my_list) != "b"] # Remove list element with !=
Delete Multiple Elements of List (3 Examples)
So far, we have always removed exactly one list element from our example list. However, in some data situations we might need to extract many list elements at once.
Fortunately, most of the R codes of the previous examples can also be used for this task. We can modify the R code of Example 1 as follows…
my_list[- c(2, 3)] # Remove multiple list elements
…the R code of Example 2 as follows…
my_list_2b <- my_list # Replicate list my_list_2b[c(2, 3)] <- NULL # Remove multiple list elements my_list_2b # Print updated list to console
…and the R code of Example 3 as follows:
my_list[names(my_list) %in% c("b", "c") == FALSE] # Remove multiple list elements
All of these syntaxes lead to a removal of the list elements b and c, but of cause you could delete as many list elements at the same time as you want.
Further Resources on the Handling of Lists
In this tutorial, I spoke a lot about subsetting list in R. If you want to learn more about the subsetting of lists in general, I can recommend the following video of the Data Camp YouTube channel:
Furthermore, you might want to have a look at some of the other tutorials of this website:
By now, I hope you have learned how to remove elements of lists in R. However, if you have any comments or feedback, just let me know in the comments below! Also, don’t forget to subscribe to my statistics newsletter for regular updates about new tutorials.