In this tutorial, we will look at how to remove the first value from a list in R with the help of some examples.
How to remove the first value in a List in R?
You can remove the first value from a list in R by setting the value at the first index in the list to NULL
. The following is the syntax –
# remove first value from list ls[1] <- NULL
You can also use the above syntax to remove any value from a list using its index. Just replace 1 with the index of the value you want to remove from the list.
Examples
Let’s look at some examples of removing the first value from a list in R.
First, let’s create a list with four values and remove the first value using the syntax mentioned above.
# create a list ls <- list("a", "b", "c", "d") # remove the first element ls[1] <- NULL # display the list print(ls)
Output:
[[1]] [1] "b" [[2]] [1] "c" [[3]] [1] "d"
Here, we create a list of four character values and then set the value at the first index in the list to NULL
. We then print the list. You can see that the list now does not have the first value from the original list.
Alternatively, you can use negative indexing to exclude values at certain indexes (for our use case, remove the first value). 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.
Let’s remove the first element from the same list used in the example above. A list of four character values.
# create a list ls <- list("a", "b", "c", "d") # remove the first element ls <- ls[-1] # display the list print(ls)
Output:
[[1]] [1] "b" [[2]] [1] "c" [[3]] [1] "d"
You can see that we get the same result as above.
You might also be interested in –
- Append Element to a List in R
- Get Length of a List in R (With Examples)
- Combine Two or More Lists Into One in R
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.