How do I order by row.names in dataframe

后端 未结 2 2035
傲寒
傲寒 2021-01-16 17:31

This is rather tricky, and it doesn\'t seem to require the same approach for ordering by a typical column in a data frame. I have something that looks like this:

相关标签:
2条回答
  • 2021-01-16 18:11

    I've had this problem before. Here's how I solved it. Using some iris sample data from base R,

    > dat <- iris[sample(nrow(iris), 10), ]
    > rn <- rownames(dat)
    > dat[order(as.numeric(rn)), ]
    
    0 讨论(0)
  • 2021-01-16 18:15

    Ordering by a column should be as simple as:

     dataframename[ order(dataframename$row.name) , ]
    

    Notice that your first column is named row.names but it's not clear if you meant that picture to refer to a dataframe where the rownames were real R rownames and not the first column. Assuming that is that "row.name" == names(dataframename)[1]. Please check whether this condition is true. At the moment you have given only a picture, whereas it would have been more helpful to provide the results of dput( dataframename[ 1:22, ])

    0 讨论(0)
提交回复
热议问题