## 2011-07-02

### R memo: elements' positions in vector under conditions

First, make a vector.
> x <- c(10,20,30)
> x
[1] 10 20 30

If you want to get positions of the elements in vector x which meet a condition, for example, x > 10, you can do like this:
> (1:length(x))[x > 10]
[1] 2 3

The second and third elements meet the condition.

The first part,
> (1:length(x))
[1] 1 2 3

gives you a vector which counts from 1 up to the number of the elements in x.

The last part,
> x > 10
[1] FALSE  TRUE  TRUE

gives you a vector which shows if each element meets the condition (greater than 10).

Then, you can get only the elements which correspond to TRUE by
> (1:length(x))[x > 10]


c.f. 13. ベクトル要素へのアクセス