- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the index of an element in a vector in R?
There are three ways to find the index of an element in a vector.
Example
> x <- sample(1:10) > x [1] 8 10 9 6 2 1 4 7 5 3
Using which
> which(x == 6)[[1]] [1] 4
Here we found the index of 6 in vector x.
Using match
> match(c(4,8),x) [1] 7 1
Here we found the index of 4 and 8 in vector x.
Using which with %in% > which(x %in% c(2,4)) [1] 5 7
Here we found the index of 2 and 4 in vector x.
Advertisements