[R] how to implement a circular buffer with R

Adrian Dusa dusa.adrian at gmail.com
Sun May 24 20:54:19 CEST 2009


Still not elegant, but I would split the string first:
spl.str <- unlist(strsplit("12345abcdefgh12345abcdefgh", ""))

Measure its length:
len.str <- length(spl.str)

Shift it:
spl.str <- c(spl.str[len.str], spl.str[seq(len.str - 1)])

Then paste it back together:
paste(spl.str, collapse="")  # "h12345abcdefgh12345abcdefg"

Shift it again (same command):
spl.str <- c(spl.str[len.str], spl.str[seq(len.str - 1)])

Paste it again (same command):
paste(spl.str, collapse="")  # "gh12345abcdefgh12345abcdef"

And so on.

Hth,
Adrian


milton ruser wrote:
> 
> Hi Maura,
> 
> It is not "elegant" but may work.
> 
> 
> actual.string<- "12345abcdefgh12345abcdefgh"
> actual.string
> actual.string<-paste(substr(actual.string,
> nchar(actual.string),nchar(actual.string)),
>    substr(actual.string, 1,nchar(actual.string)-1), sep="")
> actual.string
> 
> 
> #in a looping
> 
> actual.string<- "12345abcdefgh12345abcdefgh"
> number.buffers<-10
> my.buffers<-actual.string
> for (i in 1:number.buffers)
>  {
>  actual.string<-paste(substr(actual.string,
> nchar(actual.string),nchar(actual.string)),
>    substr(actual.string, 1,nchar(actual.string)-1), sep="")
>  my.buffers<-c(my.buffers, actual.string)
>  }
> my.buffers
> 
> Ciao,
> 
> milton
> brazil=toronto
> On Sun, May 24, 2009 at 1:09 PM, <mauede at alice.it> wrote:
> 
>> Some wavelet analysis experts have implemented periodic boundary
>> conditions
>> for signals.
>> I need to implement a circular buffer. Something like:
>> "12345abcdefgh12345abcdefgh"
>>  so that at each step the riightmost element is moved to the leftmost
>> index
>> and everything else is properly shifted:
>> "h12345abcdefgh12345abcdefg", "gh12345abcdefgh12345abcdef", ....
>>
>> My implementation (still debugging) seems to start working but is
>> terribly
>> clumsy.
>> I am sure that some expert can suggest a more elegant solution,
>> Thank you.
>> Maura
>>
>>
>>
>> tutti i telefonini TIM!
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-implement-a-circular-buffer-with-R-tp23695934p23696838.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list