R - Range of number sequences, or how can I get the reverse of 1:10

本秂侑毒 提交于 2020-04-30 07:14:05

问题


I can't seem to find an elegant solution to finding ranges. For me, it would come down to this:

> seq(1:10)
  [1]  1  2  3  4  5  6  7  8  9 10

I would like to get the reverse:

function(c(1,2,3,4,5,6,7,8,9,10))
result 1:10

Real world problem is that I have 1200 indices, some are 0, some are 1:

c(0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1)

And I would like the ranges/coordinates within the vector for each set of 0s and 1s.


回答1:


Will this simple solution work?

> rev(seq(1:10))
 [1] 10  9  8  7  6  5  4  3  2  1

> range(seq(1:10))
[1]  1 10


来源:https://stackoverflow.com/questions/48411151/r-range-of-number-sequences-or-how-can-i-get-the-reverse-of-110

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!