R表中作为标题的第一列
有没有办法将表的第一列转换为标题?例如,给定使用以下脚本获得的此表:R表中作为标题的第一列,r,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,]
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
谢谢重命名行后,您可以使用
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
>测试行名(测试)测试为.matrix(测试)
[,1]
52 7520
61 19805
69 420286
71 1066945
72 6467280
124 3983
141 137259
142 151408
143 1128764
144 2845182
重命名行后,可以使用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
>测试行名(测试)测试为.matrix(测试)
[,1]
52 7520
61 19805
69 420286
71 1066945
72 6467280
124 3983
141 137259
142 151408
143 1128764
144 2845182
@本杰明:如果你是反对票的来源,我认为你应该将其逆转。正如亚历克斯所展示的,即使是矩阵对象也能满足OP的要求。@Benjamin:如果你是否决票的来源,我认为你应该将其撤销。正如Alex所示,即使使用矩阵对象,也可以满足OP的请求。