Get a seq() in R with alternating steps

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

    I arrived the solution by: 1. Use cumsum with a vector c(from,rep(by,times),...), with by repeated times = ceiling((to-from)/sum(by)) times. 2. Truncate the sequence by !(seq > to).

     seq_alt <- function(from, to, by) {
       seq <- cumsum(c(from,rep(by,ceiling((to-from)/sum(by)))))
       return(seq[! seq > to])
     }
    

提交回复
热议问题