使用中缀运算符按列排序

时间:2017-01-16 16:13:45

标签: r

这可能是一个非常简单的问题,但我无法找到答案。我尝试在我的矩阵上应用abs,然后在第一列(降序)下应用order

在单独的行中,它看起来像:

pcaRotaMat <- abs(pcaImportance$rotation)
temp <- pcaRotaMat[order(-pcaRotaMat[,1]),]

但是,当我尝试使用中缀运算符(%>%)时,我收到以下错误:

t <- pcaImprtance$rotation %>% abs() %>% order(-[,1],)
  

错误:意外&#39; [&#39; in&#34; t&lt; - pcaImprtance $ rotation%&gt;%abs()%&gt;%order([&#34;

我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果你对更冗长的事情感到满意:

sort_fn = function(x) { 
    x[order(-x[ ,1]), ] 
}

t <- pcaImprtance$rotation %>% abs() %>% sort_fn

选项2: 如果您不想创建要排序的功能:

t <- pcaImprtance$rotation %>% abs %>% .[order(-.[, 1]), ]

&#34;&#34;是矩阵的占位符。我也不建议将变量分配给&#34; t&#34;,因为这是转换矩阵的函数。