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.
# 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.