2016-11-24 55 views
0

我已在應用上排功能r中提取山坳名(不是值)

mat <- structure(c(56L, 45L, 64L, 43L, 67L, 17L, 164L, 158L, 150L,177L,111L, 11L, 10L, 9L, 10L), .Dim = c(5L, 3L), .Dimnames = list(c("Bob", "Sam", "Bill", "Mary", "Ted"), c("X1", "X2", "X3" 
))) 

我用這個按行排序如下:

apply(mat, 1, sort) 

然而,不但得不到VALUES ,我想列名。這可能嗎?輸出需要看這個:

mat.res <- structure(c("X2", "X1", "X3", "X3", "X1", "X2", "X3", "X1", "X2", "X3", "X1", "X2", "X3", "X1", "X2"), .Dim = c(3L, 5L), .Dimnames = list(
NULL, c("Bob", "Sam", "Bill", "Mary", "Ted"))) 

回答

2

這是你在找什麼?

apply(mat,1,function(x){names(sort(x))}) 
+0

謝謝,就是這樣。 –