2013-03-19 71 views
0

所以就这么简单,谢谢。嵌入函数后排序矩阵R

我想不出一个可以帮助我排序嵌入函数的结果矩阵的一般函数。

例如:

moustache <- embed(c(1:4),3) 
    moustache 
      [,1] [,2] [,3] 
    [1,] 3 2 1 
    [2,] 4 3 2 

I would rather like the matrix : 
      [,1] [,2] [,3] 
    [1,] 1 2 3 
    [2,] 2 3 4 

感谢您的帮助。

+1

目前还不清楚,你的排序规则其实就是。也许你想'髭[,命令(小胡子[1,])]''。 – Roland 2013-03-19 13:02:08

+0

嵌入式给了我一个相反的时间序列,我只想把它放回原来的顺序 – user1627466 2013-03-19 13:20:49

回答

2

你可以使用标准的索引做到这一点:

embed(1:4,3)[,3:1] 
     [,1] [,2] [,3] 
[1,] 1 2 3 
[2,] 2 3 4 
0

这样的工作排序任何矩阵:

t(apply(moustache,1,sort)) 
1

扭转列:

moustache[ , ncol(moustache):1]