Get a seq() in R with alternating steps

前端 未结 6 1257
礼貌的吻别
礼貌的吻别 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条回答
  •  Happy的楠姐
    2020-12-17 21:29

    Here is another idea,

    fun1 <- function(x, y, j, z){
    if(j >= y) {return(x)}else{
      s1 <- seq(x, y, j+z)
      s2 <- seq(x+j, y, j+z)
      return(sort(c(s1, s2)))
      }
    }
    
    fun1(1, 19, 2, 4)
    #[1]  1  3  7  9 13 15 19
    
    fun1(1, 40, 4, 3)
    #[1]  1  5  8 12 15 19 22 26 29 33 36 40
    
    fun1(3, 56, 7, 10)
    #[1]  3 10 20 27 37 44 54
    
    fun1(1, 2, 2, 4)
    #[1] 1
    

提交回复
热议问题