Re: [R] another matrix problem

2009-07-11 Thread Erich Neuwirth
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 wh

Re: [R] another matrix problem

2009-07-05 Thread William Simpson
Thanks very much Gabor Bill On Sun, Jul 5, 2009 at 4:44 PM, Gabor Grothendieck 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 wrote: >> Try this (ignore the warning): >> >> k <- 3 >> m

Re: [R] another matrix problem

2009-07-05 Thread Gabor Grothendieck
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 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 wrote: >> I want a function t

Re: [R] another matrix problem

2009-07-05 Thread Henrique Dallazuanna
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 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

Re: [R] another matrix problem

2009-07-05 Thread Gabor Grothendieck
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 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 >

Re: [R] another matrix problem

2009-07-05 Thread David Winsemius
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 } ) Da

[R] another matrix problem

2009-07-05 Thread William Simpson
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