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
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
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 } )
Try this (ignore the warning):
k - 3
matrix(1:5, 9, k)[1:5, ]
On Sun, Jul 5, 2009 at 8:54 AM, William
Simpsonwilliam.a.simp...@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
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.simp...@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
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
Grothendieckggrothendi...@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
Thanks very much Gabor
Bill
On Sun, Jul 5, 2009 at 4:44 PM, Gabor
Grothendieckggrothendi...@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
Grothendieckggrothendi...@gmail.com wrote:
Try this (ignore
7 matches
Mail list logo