Remove zeros in the start and end of a vector

前端 未结 4 620
鱼传尺愫
鱼传尺愫 2021-01-12 13:58

I have a vector like this:

x <-  c(0, 0, 0, 0, 4, 5, 0, 0, 3, 2, 7, 0, 0, 0)

I want to keep only the elements from position 5 to 11. I w

4条回答
  •  鱼传尺愫
    2021-01-12 14:11

    This would also work :

    x[cumsum(x) & rev(cumsum(rev(x)))]
    # [1] 4 5 0 0 3 2 7
    

提交回复
热议问题