[R] how to implement a circular buffer with R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon May 25 11:22:33 CEST 2009


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

not sure if this is what you want, but it might provide a hint:


    circularize = function(string) {
       name = deparse(substitute(string))
       index = 0
       length = nchar(string)
       rm(list=name, envir=parent.frame())
       makeActiveBinding(
          name,
          function(value, ...)
             if (missing(value)) {
                index <<- index %% length + 1
                if (index == 1) string
                else paste(
                    substr(string, index, length),
                    substr(string, 1, index-1),
                    sep='') }
             else
                stop(sprintf('cannot assign to circularized string
"%s"', name)),
          parent.frame()) }

    string = 'foo'
    circularize(string)

    string
    # "foo"
    string
    # "oof"
    string
    # "ofo"
    string
    # "foo"
    # ...


> 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]]




More information about the R-help mailing list