0

私は行列を持っていて、行を並べ替えて、たとえば行 5 を行 2 に切り替え、行 2 を行 7 に切り替えることができるようにしたい\nと考えています。 (そのtxtファイル)次に、マトリックスの名前を使用します(私の場合は「k」のようにしますk[txt file,]-> k_new が、識別子は最初の列ではなく、行名として定義されているため、これは機能しません.

4

1 に答える 1

1
k[ c(1,5,3,4,7,6,2), ] #But probably not what you meant....

または、おそらく (「k」オブジェクトの行名がデフォルトの文字と数字のシーケンス以外の場合):

k[ char_vec , ]   # where char_vec will get matched to the row names.

(dat <- structure(list(person = c(1, 1, 1, 1, 2, 2, 2, 2), time = c(1, 
2, 3, 4, 1, 2, 3, 4), income = c(100, 120, 150, 200, 90, 100, 
120, 150), disruption = c(0, 0, 0, 1, 0, 1, 1, 0)), .Names = c("person", 
"time", "income", "disruption"), row.names = c("h", "g", "f", 
"e", "d", "c", "b", "a"), class = "data.frame"))

 dat[ c('h', 'f', 'd', 'b') , ]
 #-------------
  person time income disruption
h      1    1    100          0
f      1    3    150          0
d      2    1     90          0
b      2    3    120          1
于 2012-08-30T20:08:15.837 に答える