A list is a one-dimensional data structure that can store values of different types together. When working with lists, it can be handy to know how to perform common operations such as removing elements. In this tutorial, we will look at how to remove an element from a list in R with the help of some examples.
How to remove elements from a List in R?
To remove an element from a list in R, set the value at the element’s index in the list to NULL
. The following is the syntax –
# remove element at index i ls[i] <- NULL
There are other ways as well to remove a value from a list. For example, you can use negative indexing or use a logical condition to remove the element.
Examples
Let’s now look at some examples of using the different methods mentioned above.
Remove element by index from a list
To remove an element by an element’s index, set the list value at that index to NULL
. For example, let’s remove the element at the 4th index from a list of five elements.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# create a list ls <- list("a", "b", "c", "d", "e") # remove element at index 4 from the list ls[4] <- NULL # display the list print(ls)
Output:
[[1]] [1] "a" [[2]] [1] "b" [[3]] [1] "c" [[4]] [1] "e"
Here, we set the list value at index 4 to be NULL
, you can see that the original element at index 4, “d”, is not present in the list.
Alternatively, you can use negative indexing to exclude values at certain indexes. Note that this does not modify the list in place. To modify the original list, assign the result from the negative indexing to the original list variable.
For example, let’s perform the same operation as above.
# create a list ls <- list("a", "b", "c", "d", "e") # remove element at index 4 from the list ls <- ls[-4] # display the list print(ls)
Output:
[[1]] [1] "a" [[2]] [1] "b" [[3]] [1] "c" [[4]] [1] "e"
Here, we remove the element at index 4 from the original list. You can see that we get the same result as above.
Remove element by value from a list
You can also remove an element by value from a list. For this, use logical indexing where you only select the elements from the list that are not equal to the value you want to remove.
We’ll use the same list as used in the examples above and remove the element with the value “d”.
# create a list ls <- list("a", "b", "c", "d", "e") # remove element with value "d" from the list ls <- ls[ls != "d"] # display the list print(ls)
Output:
[[1]] [1] "a" [[2]] [1] "b" [[3]] [1] "c" [[4]] [1] "e"
The resulting list does not have the value “d”.
Note that filtering a list using logical indexing does not modify the list in place. If you want to modify the original list, assign the result from the filtering to the original list variable (as shown in the above example).
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.