第一列作为R表中的标题

时间:2012-01-16 14:29:55

标签: r

有没有办法将表格的第一列变成标题?例如,给定此表使用以下脚本获得:

 test <- as.matrix(read.csv(file="fileName.csv", sep=",", head=FALSE))

  [1,]  72 6467280
  [2,]  71 1066945
  [3,] 143 1128764
  [4,]  69  420286
  [5,] 141  137259
  [6,] 144 2845182
  [7,] 142  151408
  [8,]  61   19805
  [9,]  52    7520
 [10,] 124    3983

我想获得一个表格,其中第一列是标签,并且能够根据这些值重新排列表格。所以我会得到这样的东西。

  [52,]  7520
  [61,]  19805
  [69,]  420286
  [71,]  1066945
  [72,]  6467280
  [124,] 3983
  [141,] 137259
  [142,] 151408
  [143,] 1128764
  [144,] 2845182

由于

1 个答案:

答案 0 :(得分:6)

重命名行后,您可以使用row.names()检索它们,并应用所需的排序:

> test <- as.matrix(read.csv("http://dl.dropbox.com/u/31495717/stackoverlow.orderlist.csv", sep=",", head=FALSE))
> rownames(test) <- test[,1]
> test <- test[order(as.numeric(row.names(test)), decreasing=FALSE),]
> test <- test[,-1]
> as.matrix(test)
       [,1]
52     7520
61    19805
69   420286
71  1066945
72  6467280
124    3983
141  137259
142  151408
143 1128764
144 2845182
相关问题