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}.

Wednesday, 10 March 2010

Append to a vector in regular intervals efficiently in R

In many instances one may need to append a character or a value in a given sequence, usually represented as a vector or array. It is pretty easy to achieve this naively. However, the point is not to consume any more memory. This is one of the simple solutions I can propose in GNU R.

# This function appends a word into vector a in every f element
appendfreq<-function(a,f,word) { i <- as.integer(length(a)/f-1); q<-f; for(j in 1:i) { a<-append(a,word,after=q); q<-q+f+1; } ;return(a); }


(c) Copyright 2008-2017 Mehmet Suzen (suzen at acm dot org)

Creative Commons Licence
This work is licensed under a Creative Commons Attribution 3.0 Unported License.