I want a function that takes an input vector, the number of columns and returns a matrix as follows. x<- 1:5 foo(x, nc=3) 1 5 4 2 1 5 3 2 1 4 3 2 5 4 3 Thanks again for any help. Bill
On Jul 5, 2009, at 8:54 AM, William Simpson wrote:> I want a function that takes an input vector, the number of columns > and returns a matrix as follows. > > x<- 1:5 > > foo(x, nc=3) > > 1 5 4 > 2 1 5 > 3 2 1 > 4 3 2 > 5 4 3 >See if this gives you any ideas: sapply(1:3, function(z) { ((x - z) %% 5) +1 } ) David Winsemius, MD Heritage Laboratories West Hartford, CT
Try this (ignore the warning): k <- 3 matrix(1:5, 9, k)[1:5, ] On Sun, Jul 5, 2009 at 8:54 AM, William Simpson<william.a.simpson at gmail.com> wrote:> I want a function that takes an input vector, the number of columns > and returns a matrix as follows. > > x<- 1:5 > > foo(x, nc=3) > > 1 5 4 > 2 1 5 > 3 2 1 > 4 3 2 > 5 4 3 > > Thanks again for any help. > > Bill > > ______________________________________________ > 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. >
Try this: foo <- function(x, ncols){ tail(embed(rep(x, ncols), ncols), length(x)) } foo(x, 3) On Sun, Jul 5, 2009 at 9:54 AM, William Simpson <william.a.simpson@gmail.com> wrote:> I want a function that takes an input vector, the number of columns > and returns a matrix as follows. > > x<- 1:5 > > foo(x, nc=3) > > 1 5 4 > 2 1 5 > 3 2 1 > 4 3 2 > 5 4 3 > > Thanks again for any help. > > Bill > > ______________________________________________ > 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]]
In terms of x this is: k <- 3 x <- 1:5 n <- length(x) matrix(x, 2*n-1, k)[1:n, ] On Sun, Jul 5, 2009 at 10:01 AM, Gabor Grothendieck<ggrothendieck at gmail.com> wrote:> Try this (ignore the warning): > > k <- 3 > matrix(1:5, 9, k)[1:5, ] > > > On Sun, Jul 5, 2009 at 8:54 AM, William > Simpson<william.a.simpson at gmail.com> wrote: >> I want a function that takes an input vector, the number of columns >> and returns a matrix as follows. >> >> x<- 1:5 >> >> foo(x, nc=3) >> >> 1 5 4 >> 2 1 5 >> 3 2 1 >> 4 3 2 >> 5 4 3 >> >> Thanks again for any help. >> >> Bill >> >> ______________________________________________ >> 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. >> >
Thanks very much Gabor Bill On Sun, Jul 5, 2009 at 4:44 PM, Gabor Grothendieck<ggrothendieck at gmail.com> wrote:> In terms of x this is: > > k <- 3 > x <- 1:5 > > n <- length(x) > matrix(x, 2*n-1, k)[1:n, ] > > On Sun, Jul 5, 2009 at 10:01 AM, Gabor > Grothendieck<ggrothendieck at gmail.com> wrote: >> Try this (ignore the warning): >> >> k <- 3 >> matrix(1:5, 9, k)[1:5, ] >> >> >> On Sun, Jul 5, 2009 at 8:54 AM, William >> Simpson<william.a.simpson at gmail.com> wrote: >>> I want a function that takes an input vector, the number of columns >>> and returns a matrix as follows. >>> >>> x<- 1:5 >>> >>> foo(x, nc=3) >>> >>> 1 5 4 >>> 2 1 5 >>> 3 2 1 >>> 4 3 2 >>> 5 4 3 >>> >>> Thanks again for any help. >>> >>> Bill >>> >>> ______________________________________________ >>> 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. >>> >> >
rotateleft<-function(vec,offset) vec[(((0:(length(vec)-1))+offset) %% length(vec)) + 1] rotateright<-function(vec,offset)rotateleft(vec,length(vec)-offset) rotatedcols<- function (vec ,offsets)do.call(cbind,lapply(offsets,function(x)rotateright(vec,x))) rotatedcols(1:5,0:2) is perhaps what you want On Jul 5, 2009, at 2:54 PM, William Simpson wrote:> I want a function that takes an input vector, the number of columns > and returns a matrix as follows. > > x<- 1:5 > > foo(x, nc=3) > > 1 5 4 > 2 1 5 > 3 2 1 > 4 3 2 > 5 4 3 > > Thanks again for any help. > > Bill > > ______________________________________________ > 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. >-- Erich Neuwirth Didactic Center for Computer Science and Institute for Scientific Computing University of Vienna