[R] another matrix problem

David Winsemius dwinsemius at comcast.net
Sun Jul 5 15:47:20 CEST 2009


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




More information about the R-help mailing list