How can I write a function f(v, n)
(or use a base R function) that turns a numeric vector v
into another based on n
, a recycling factor (in lack of a better word).
For instance:
f(v = c(1,2,3,4,5), n = 1) #would yield c(1,2,3,4,5)
f(v = c(1,2,3,4,5), n = 2) #would yield c(1,2,1,2,1)
f(v = c(1,2,3,4,5), n = 3) #would yield c(1,2,3,1,2)
f(v = c(5,4,3,2,1), n = 3) #would yield c(2,1,3,2,1)
f(v = c(3,6), n = 3) #would yield c(3,3)
The closest I got was to use %%
1:5%%3 #giving me: [1] 1 2 0 1 2 - not quite what I want, at least some recycling.