簡體   English   中英

使用中綴運算符按列排序

[英]Order by column using infix operator

這可能是一個非常簡單的問題,但我找不到答案。 我試圖在我的矩陣上應用abs然后按第一列(降序)應用order

在單獨的行中,它看起來像:

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

但是,當我嘗試使用中綴運算符 ( %>% ) 時,出現以下錯誤:

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

錯誤:“t <- pcaImprtance$rotation %>% abs() %>% order([”

您的幫助將不勝感激。

如果您對更詳細的內容感到滿意:

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

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

選項 2:如果您不想創建排序函數:

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

“。” 是矩陣的占位符。 我也不建議將變量分配給“t”,因為這是轉置矩陣的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM