Vivek Khetan
Vivek Khetan

Reputation: 191

R- How to get locations/positions of elements of a vector that satisfies given conditions?

I am using R studio with R 3.1.2 on Mac ox X 10.10.3, i need to get positon of elments in a vector when it is given some conditions.

Let x be a sample of 100 elements and y is equals to 100. How to get a vector stating positions of elements of x, for which x >y

I have tried functions like match, replace and %in%, but not much success. Either i am missing something oe doing it all wrong.

Thanks and Regards.

Upvotes: 1

Views: 68

Answers (1)

akrun
akrun

Reputation: 886938

If you need the first position where x >y, try which.max

which.max(x>y)
#[1] 6

It could be also done with which

 which(x >y)[1]
 #[1] 6

data

set.seed(24)
x <- sample(1:140, 100, replace=TRUE)
y <- 100

Upvotes: 2

Related Questions