I would like to simply recycle a vector to a given length (see below). The below function is inspired by OpenRepGrid::recycle, but I struggle to believe that this is the best way to get there, given that recycling is so fundamental.
I am looking for a pure base R solution, and it should be simpler/ faster/ more efficient than the approach below.
recycle <- function(x, new_length){
rep(x, ceiling(new_length/length(x)))[1:new_length]
}
## desired effect
recycle(c("red", "blue", "green"), 10)
#> [1] "red" "blue" "green" "red" "blue" "green" "red" "blue" "green"
#> [10] "red"