I need to decompose the vector into a series of x and repeat, I'm not quite sure what the correct term is for this. This is the inverse of the rep
function. So the vector
[1,2,2,2,2,1,1,1,1,1,2,2] -> [1x1, 4x2, 5x1, 2x2]
I wrote a small function for this, but I'm sure there should be a more proprietary way:
invrep <- function(y){ numy <- as.numeric(y); newpoints <- which(c(T,diff(numy) != 0)); x <- y[newpoints]; times <- diff(c(newpoints, length(numy)+1)); return(list(x=x, times=times)); } myvec <- factor(floor(runif(50,0,3)), levels=0:2, labels=c("blue", "yellow", "red")); myrep <- invrep(myvec); identical(myvec, rep(myrep$x, myrep$times));
source share