Extract every nth element of a vector

后端 未结 4 1973
醉酒成梦
醉酒成梦 2020-11-28 04:07

I would like to create a vector in which each element is the i+6th element of another vector.

For example, in a vector of length 120 I want to create an

4条回答
  •  猫巷女王i
    2020-11-28 04:22

    I think you are asking two things which are not necessarily the same

    I want to extract every 6th element of the original

    You can do this by indexing a sequence:

    foo <- 1:120
    foo[1:20*6]
    

    I would like to create a vector in which each element is the i+6th element of another vector.

    An easy way to do this is to supplement a logical factor with FALSEs until i+6:

    foo <- 1:120
    i <- 1
    foo[1:(i+6)==(i+6)]
    [1]   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119
    
    i <- 10
    foo[1:(i+6)==(i+6)]
    [1]  16  32  48  64  80  96 112
    

提交回复
热议问题