Display the position of a given value on a vector in R

Asked

Viewed 3,210 times

3

I started studies in R a short time ago and I have studied this language a lot. However, I am having difficulties in how to get the position of a vector, that is, the order in which a certain value is recorded in this vector.

Ex.: > vector <- c(15, 23, 32, 53, 64)

vector [1] 15 23 32 53 64

How to display for example the position of the value 53, (I know it is the fourth position)?

  • I’m sorry about the title, is that I had another question to ask about set.Seed and I forgot to change the title!

  • Click EDIT and correct the title, if applicable.

1 answer

4


The first way to obtain the position of a given element of a vector is by using the function match as follows match(c(15, 53), vetor). This will return the position of the elements of the vector you consider known.

The second way is using the function which as follows which(vetor %in% c(23, 53)).

For knowledge purposes, if you want to know the vector element based on a given position, you can use the command vetor[3], where 3 represents the third position. The result will be the third position element which in this case is 32.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.