In this tutorial, we will look at how to get the index of an element in an R vector with the help of some examples.
How to get the index of an element in an R vector?
You can use the
match() function in R, to get the index of an element in a vector. It returns the index of the first occurrence of the value in the vector as an integer. The following is the syntax –
# get index of element e in vector vec match(e, vec)
Pass the element (or the vector) as the first argument and the vector in which you want the know the position as the second argument.
You can also use the
match() function to get the index of multiple values at a time in a vector (see the examples below).
Let’s look at some examples of getting the index (or position) of an element in an R vector. First, we will create a vector that we will be using throughout this tutorial.
# create a vector vec <- c("a", "b", "c", "d", "c") # display the vector print(vec)
 "a" "b" "c" "d" "c"
We have a vector of some characters.
Index of the first occurrence of an element in a vector
Let’s use the
match() function to get the position of the “c” in the above vector. For this, pass “c” as the first argument, and the vector, vec as the second argument to the function.
- 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
- 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
- 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.
# get index of "c" match("c", vec)
You can see that we get the position (or index) of “c” as 3. Note that in the above vector, “c” occurs at positions 3 and 5 but we only get the index of its first occurrence (or match) with the
If you want to get all the indexes of occurrences of an element in a vector R, use the
which() function instead. Pass an equality operation (checking whether the vector value is the same as the element we want the indexes for) as the argument.
# get all indexes of "c" which(vec == "c")
You can see that we get all the positions for the element “c” in the above vector.
Index of the first occurrence of multiple elements in a vector
You can also pass a vector as the first argument to the
match() function. In such a case, the match function will get the positions of the first occurrence of each element in that vector.
For example, let’s get the positions of “a” and “c” in the above vector using the
# get index of "a" and "c" match(c("a", "c"), vec)
We the position of “a” as 1 and “c” as 3.
You might also be interested in –
- R – Get Element by Index in a Vector
- How to Print a Vector in R?
- Find Index of an Element in a Tuple in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.