Wednesday 17 March 2010

Repeat chunks of a sequence arbitrary times with GNU R

Repeating arbitrary size chunks multiple times in a given sequence with preserving order of chunks would be needed in certain computations. For example in matrix multiplication that is reduced to a 1D problem. Mapping your sequence into a matrix and repeating certain column or generating another sequence in the appropriate loop are straight forward solutions indeed. However these approaches will suffer immensely in memory consumption with increasing sequence size and would generate the original problem it seek to solve. More efficient and the general way to achieve repeating chunks is solved with the following R code:

manyrep <-function(a,f,n) {
i <- as.integer(length(a)/f);
q <- f
for(j in 1:i) {
a<-append(a,a[(q-f+1):q],after=q);
q<-q+2*f
p <- 1;
while(p <= (n-1)) {
a<-append(a,a[(q-2*f+1):(q-f)],after=(q-f));
q<-q+f ;
p<-p+1
}
}
return(a)
}

For example if sequence a = {1,2,3,4}, frequency or chunk size f=2 and number of repetition n=1, hence manyrep(1:4,2,1) the output should look like {1,2,1,2,3,4,3,4}. If n was 2 output would be {1,2,1,2,1,2,3,4,3,4,3,4}.