R - how to re-order data frame by row index number

2019-04-26 13:54发布

This may be a very basic question but I could't find it. Let's say I have a data frame d with row numbers in disorder like this:

    Signal
4   9998
3   549
1   18
5   2.342
2   0.043

How can I sort this by increasing row index numbers to obtain the following?

    Signal
1   18
2   0.043
3   549
4   9998
5   2.342

3条回答
走好不送
2楼-- · 2019-04-26 14:08

you can also use this :

 d[order(as.numeric(rownames(d))),,drop=FALSE]

drop is useful only if your data.frame has one column otherwise remove it

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-26 14:20
d <- read.table(text=readClipboard(), header=TRUE)    
d$index <- as.numeric(row.names(d))
d[order(d$index), ]
查看更多
成全新的幸福
4楼-- · 2019-04-26 14:31
rownames(d) <- 1 : length(rownames(d))
查看更多
登录 后发表回答