Vectors are used to store one-dimensional data of the same type in R. In this tutorial, we will look at how to remove the first value from a vector in R with the help of some examples.
How to remove the first value in a Vector in R?
Negative indexing can be used to exclude values using their index from a vector. To remove the first value from a vector in R, you can use the negative index -1
inside the []
notation. The following is the syntax –
# remove first value from vector vec <- vec[-1]
Note that using a negative index does not modify the vector in place. It simply filters the vector and returns a copy with the value at the given index removed. To modify the original vector, assign the resulting vector to the original vector variable.
You can similarly remove any value using its index from a vector in R.
Examples
Let’s look at some examples of removing the first value from a vector in R.
First, let’s create a vector with four values and remove the first value using the syntax mentioned above.
# create a vector vec <- c(10, 20, 30, 40) # remove the first element vec <- vec[-1] # display the vector print(vec)
Output:
[1] 20 30 40
Here, we use negative indexing to remove the value the index 1 (which is the first value in the vector). Note that we re-assign the returned vector to the variable vec
. You can see that the first value from the original vector is not present in the resulting vector.
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 look at another example. This time let’s use a vector with named values.
# create a vector vec <- c("a"=10, "b"=20, "c"=30, "d"=40) # remove the first element vec <- vec[-1] # display the vector print(vec)
Output:
b c d 20 30 40
You can see that the first value from the original vector was removed.
You might also be interested in –
- R – Remove NA Values from a Vector
- Remove First Value From a List in R
- Append Element to a Vector in R
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.