1

I have a dataframe with rownames (s1, s10, s11, s2, s3 ...) but they are not ordered numerically by rowname. I have tried df <- df[order(rownames(df)),] but the order hasn't changed. Please could you show me how to order a dataframe with rownames as described.

M--
  • 25,431
  • 8
  • 61
  • 93
  • I have just tried that now it says ```Error in gsub(rownames(df)): argument x is missing, with no default``` –  Dec 03 '20 at 14:08
  • 1
    Take a look at [this post](https://stackoverflow.com/questions/17531403/how-to-sort-a-character-vector-where-elements-contain-letters-and-numbers-in-r) for examples of ordering by numeric values within strings... – Ben Dec 03 '20 at 14:13

2 Answers2

0

If you want to keep the leading S and pad the numbers so the sort works, the formatC function will do that:

paste("S", formatC(1:20, width=2, flag="0"), sep="")
[1] "S01" "S02" "S03" "S04" "S05" "S06" "S07" "S08" "S09" "S10" "S11" "S12" "S13" "S14" "S15" "S16" "S17"
[18] "S18" "S19" "S20"

Replace the 1:20 with 1:nrow(df) of your dataframe and the width to the width of nrow

SteveM
  • 2,226
  • 3
  • 12
  • 16
0

Here's what I came up with in the tidyverse.

I create a tibble with a column containing the rownames; convert them to a factor; and order the pertaining factor levels with str_sort() which applies natural sorting. And then I sort the tibble according to these levels.

library(tidyverse)

my_df <- data.frame(x=rnorm(4))
rownames(my_df) <- c("s11", "s10", "s1", "s2")
my_df <- rownames_to_column(my_df, var="row_names")
my_df
#>   row_names          x
#> 1       s11 -1.2418258
#> 2       s10 -1.0952321
#> 3        s1 -0.4266726
#> 4        s2  1.0554337

my_df <- my_df %>% 
  mutate(names_fac=forcats::fct_relevel(row_names, stringr::str_sort(my_df$names))) %>% 
  arrange(as.numeric(names_fac))
my_df
#>   row_names          x names_fac
#> 1        s1 -0.4266726        s1
#> 2       s10 -1.0952321       s10
#> 3       s11 -1.2418258       s11
#> 4        s2  1.0554337        s2

Created on 2020-12-03 by the reprex package (v0.3.0)

zoowalk
  • 2,018
  • 20
  • 33