Florent
Florent

Reputation: 13

Position of a variable within a vector

Does anyone know of a simple and efficient way to figure out how many values, in an unsorted vector, are greater than a variable?

My vector is 1,000,000 values long, and I have about 400 of these comparisons to make, with different vectors and variables. Any time-saving function would be appreciated...

Upvotes: 1

Views: 1055

Answers (3)

giucris
giucris

Reputation: 41

if I understand what you want. you may reorder your vector (quick sort), and after you may a search(binary search). all elements, after the first element that is > then you variable, will be >. is the opposite for <.

Upvotes: -4

sam
sam

Reputation: 430

Just use the which function. So if I have vector,

vector<-c(1,2,3,4,5)
which(vector>1)

Outputs 2,3,4,5

Upvotes: 2

seancarmody
seancarmody

Reputation: 6290

If you just want to know how many meet the condition rather than which ones meet the condition, try this:

vector<-c(1,2,3,4,5)
sum(vector>1)

Upvotes: 7

Related Questions