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); }


No comments:

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

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License