> x <- c(10,20,30) > x [1] 10 20 30If 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 3The second and third elements meet the condition.
The first part,
> (1:length(x)) [1] 1 2 3gives you a vector which counts from 1 up to the number of the elements in x.
The last part,
> x > 10 [1] FALSE TRUE TRUEgives 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. ベクトル要素へのアクセス
No comments:
Post a Comment