[R] shifted window of string

From: david hilton shanabrook <davidshanabrook_at_me.com>
Date: Mon, 14 Jun 2010 23:46:30 -0400


basically I need to create a sliding window in a string. a way to explain this is:

> v <- c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y")
> window <- 5
> shift <- 2

I want a matrix of characters with "window" columns filled with "v" by filling a row, then shifting over "shift" and continuing to the next row until "v" is exhausted. You can assume "v" will evenly fit "m"

so the result needs to look like this matrix where each row is shifted 2 (in this case):

> m

      [,1] [,2] [,3] [,4] [,5]
[1,] "a" "b" "c" "d" "e"
[2,] "c" "d" "e" "f" "g"
[3,] "e" "f" "g" "h" "i"
[4,] "g" "h" "i" "j" "k"
[5,] "i" "j" "k" "l" "m"
[6,] "k" "l" "m" "n" "o"
[7,] "m" "n" "o" "p" "q"
[8,] "o" "p" "q" "r" "s"
[9,] "q" "r" "s" "t" "u"

[10,] "s" "t" "u" "v" "w"
[11,] "t" "u" "v" "w" "x"

This needs to be very efficient as my data is large, loops would be too slow. Any ideas? It could also be done in a string and then put into the matrix but I don't think this would be easier.

I will want to put this in a function:

shiftedMatrix <- function(v, window=5, shift=2){...

return(m)}

thanks

dhs

        [[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. Received on Tue 15 Jun 2010 - 04:46:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Jun 2010 - 16:30:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive