[R] another matrix problem

Erich Neuwirth erich.neuwirth at univie.ac.at
Sat Jul 11 10:36:06 CEST 2009


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




More information about the R-help mailing list