Get a seq() in R with alternating steps

前端 未结 6 1259
礼貌的吻别
礼貌的吻别 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:47

    Here is an alternative that uses diffinv This method over allocates the values, so as a stopping rule, I get the elements that are less than or equal to the stopping value.

    seqAlt <- function(start, stop, by1, by2) {
       out <- diffinv(rep(c(by1, by2), ceiling(stop / (by1 + by2))), xi=start)
       return(out[out <= stop])
    }
    
    seqAlt(1, 19, 2, 4)
    [1]  1  3  7  9 13 15 19
    

提交回复
热议问题