Elegant way to vectorize seq?

后端 未结 3 1901
醉酒成梦
醉酒成梦 2021-01-13 03:50

Despite the similar title, this is not the same question as Vectorizing rep and seq in R.

My immediate goal: Given a vector, I want to generate a new vector containi

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 04:51

    In R, one of the easiest way to vectorize a function is to use the Vectorize function.

    Basically, you can vectorize the from an to argument and give all the starter as a vector in the from argument and do the same thing for the to argument.

    Using your example, you can do something like this

    seq2 <- Vectorize(seq.default, vectorize.args = c("from", "to"))
    
    unlist(seq2(from = c(1, 1.75), to = c(2, 2.75), by = 0.25))
    
    ## [1] 1.00 1.25 1.50 1.75 2.00 1.75 2.00 2.25 2.50 2.75
    

提交回复
热议问题