Search code examples
rinterpolationdata-manipulationdata-cleaningdata-processing

Interpolation / stretching out of values in vector to a specified length


I have vectors of different length For example,

a1 = c(1,2,3,4,5,6,7,8,9,10) a2 = c(1,3,4,5) a3 = c(1,2,5,6,9)

I want to stretch out a2 and a3 to the length of a1, so I can run some algorithms on it that requires the lengths of the vectors to be the same. I would truncate a1 to be same as a2 and a3, but i end up losing valuable data.

ie perhaps a2 could look something like 1 1 1 3 3 3 4 4 5 5 ?

Any suggestions would be great! thanks

EDIT: I need it to work for vectors with duplicate values, such as c(1,1,2,2,2,2,3,3) and the stretched out values to represent the number of duplicate values in the original vector, for example if i stretched the example vector out to a length of 100 i would expect more two's than one's.


Solution

  • It sounds like you're looking for something like:

    lengthen <- function(vec, length) {
      vec[sort(rep(seq_along(vec), length.out = length))]
    }
    
    lengthen(a2, length(a1))
    # [1] 1 1 1 3 3 3 4 4 5 5
    lengthen(a3, length(a1))
    # [1] 1 1 2 2 5 5 6 6 9 9
    lengthen(a4, length(a1))
    # [1] 5 5 5 1 1 1 3 3 4 4
    lengthen(a5, length(a1))
    # [1] 1 1 1 1 1 1 4 4 5 5
    

    Where:

    a1 = c(1,2,3,4,5,6,7,8,9,10)
    a2 = c(1,3,4,5)
    a3 = c(1,2,5,6,9)
    a4 = c(5,1,3,4)
    a5 = c(1,1,4,5)