Get a seq() in R with alternating steps

前端 未结 6 1282
礼貌的吻别
礼貌的吻别 2020-12-17 21:19

The seq function in R would give me a sequence from x to y with a constant step m:

seq(x, y, m)
         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 21:42

    perfect example of recycling vectors in R

    # 1. 
    x = 1; y = 19; m1 = 2; m2 = 4
    (x:y)[c(TRUE, rep(FALSE, m1-1), TRUE, rep(FALSE,m2-1))]
    # [1]  1  3  7  9 13 15 19
    # 2.
    x = 3; y = 56; m1 = 7; m2 = 10
    (x:y)[c(TRUE, rep(FALSE, m1-1), TRUE, rep(FALSE,m2-1))]
    # [1]  3 10 20 27 37 44 54
    

提交回复
热议问题