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
As @TylerRinker suggested in a comment on my question, for my particular needs there's a solution that's simpler than what's suggested by my formulation of the problem--I was too focused on a particular way of thinking about it. Since I want regularly spaced numbers interpolated between numbers that are already regularly spaced, I can just apply seq
to the initial and terminal values in the vector, using a by
value that divides evenly into the interval between values in the the original vector:
subdiv <- function(x, by) seq(x[1], x[length(x)], by)
subdiv(1:4, .25)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00
subdiv(c(-0.20269232, -0.06500001, 0.07269230), 0.1376923/3)
[1] -0.20269232 -0.15679489 -0.11089745 -0.06500002 -0.01910259 0.02679485 0.07269228
where 0.1376923 is the difference between successive elements in the second application. This solution is OK as long as I don't need that the elements carried over from the original vector to be exactly equal to their original values--as you can see, there are some differences due to floating point arithmetic. (What I'm actually doing is constructing bins for histograms; the precise boundaries won't matter.)
@dickoa's and @geektrader's answers have broader use, though, and preserve the exact values of the original numbers from the input vector.