Find Element Position in R Vector

In R language, the which() function can be used to find the position of a specific element in a vector.

For example, if we have a vector vec <- c(3, 6, 9, 12, 15), we can use the following code to find the position of the element 9 in the vector:

vec <- c(3, 6, 9, 12, 15)
index <- which(vec == 9)
print(index)

This will result in 3, indicating that the element 9 is in the third position in the vector vec. If there are multiple identical elements in the vector, the which() function will return the positions of all of them.

bannerAds