Ron Shefi
2010-Jun-22 06:35 UTC
[R] Generate a list of all subsequence of length k from an array
Hi, I would like to generate a list of all subsequence of length k from an array with length n (k < n). The result should be organized in a matrix. So the matrix should look like the following whereas each row is one of a subsequence of len k. [a[1:k], a[2:(k+1)] a[3:(k+2)] ... a[(n-k+1):n] Is there away to do this with sapply method? thanks, Ron _________________________________________________________________ The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail. ID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4 [[alternative HTML version deleted]]
David Winsemius
2010-Jun-22 14:49 UTC
[R] Generate a list of all subsequence of length k from an array
On Jun 22, 2010, at 2:35 AM, Ron Shefi wrote:> > Hi, > I would like to generate a list of all subsequence of length k from > an array with length n (k < n). The result should be organized in a > matrix. > So the matrix should look like the following whereas each row is one > of a subsequence of len k. > > [a[1:k], > a[2:(k+1)] > a[3:(k+2)] > ... > a[(:n] > > Is there away to do this with sapply method?I suppose you could try (untested in absence of example): sapply(1:(n-k+1), function(x) { a[x:(x+k)] }) -- David Winsemius, MD West Hartford, CT
Henrique Dallazuanna
2010-Jun-22 14:56 UTC
[R] Generate a list of all subsequence of length k from an array
You could try this: embed(a, k) On Tue, Jun 22, 2010 at 3:35 AM, Ron Shefi <ron380@hotmail.com> wrote:> > Hi, > I would like to generate a list of all subsequence of length k from an > array with length n (k < n). The result should be organized in a matrix. > So the matrix should look like the following whereas each row is one of a > subsequence of len k. > > [a[1:k], > a[2:(k+1)] > a[3:(k+2)] > ... > a[(n-k+1):n] > > Is there away to do this with sapply method? > > thanks, > Ron > > _________________________________________________________________ > The New Busy is not the too busy. Combine all your e-mail accounts with > Hotmail. > > ID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4 > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]