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]]


