Home > Net >  sort colum name with character and number
sort colum name with character and number

Time:10-28

I have this df:

table
              C1 C10 C11 C12 C13 C14 C15 C16 C17 C18 C2 C3 C4 C5 C6 C7 C8 C9
Mest           1   1   1   1   0   0   0   0   0   0  0  0  0  0  0  0  0  0
Dlk1           0   0   0   0   0   0   0   0   0   0  0  0  1  0  0  0  0  0
Meg3           0   0   0   0   0   0   0   0   0   0  0  0  0  0  0  0  0  0

I'd like to order the columns, I've tried:

colnames(table) <- stringr::str_sort(colnames(table),numeric = TRUE) 

but It only changes the name of columns in alphabetic order leaving the column in the same position:

table
               C1  C2  C3 C4  C5  C6  C7  C8  C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
Mest           1   1   1   1   0   0   0   0   0   0   0   0   0   0   0  0   0   0
Dlk1           0   0   0   0   0   0   0   0   0   0   0   0   1   0   0  0   0   0
Meg3           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  0   0   0

CodePudding user response:

Base R,

DAT[,order(as.integer(sub("\\D", "", names(DAT))))]
#      C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
# Mest  1  0  0  0  0  0  0  0  0   1   1   1   0   0   0   0   0   0
# Dlk1  0  0  0  1  0  0  0  0  0   0   0   0   0   0   0   0   0   0
# Meg3  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0

CodePudding user response:

Here is a another stringr solution. It uses str_order, not str_sort.

table <- table[stringr::str_order(names(table), numeric = TRUE)]
table
#>      C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
#> Mest  1  0  0  0  0  0  0  0  0   1   1   1   0   0   0   0   0   0
#> Dlk1  0  0  0  1  0  0  0  0  0   0   0   0   0   0   0   0   0   0
#> Meg3  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0

Created on 2022-10-27 with reprex v2.0.2


Data

table <-
structure(list(C1 = c(1L, 0L, 0L), C10 = c(1L, 0L, 0L), C11 = c(1L, 
0L, 0L), C12 = c(1L, 0L, 0L), C13 = c(0L, 0L, 0L), C14 = c(0L, 
0L, 0L), C15 = c(0L, 0L, 0L), C16 = c(0L, 0L, 0L), C17 = c(0L, 
0L, 0L), C18 = c(0L, 0L, 0L), C2 = c(0L, 0L, 0L), C3 = c(0L, 
0L, 0L), C4 = c(0L, 1L, 0L), C5 = c(0L, 0L, 0L), C6 = c(0L, 0L, 
0L), C7 = c(0L, 0L, 0L), C8 = c(0L, 0L, 0L), C9 = c(0L, 0L, 0L
)), row.names = c("Mest", "Dlk1", "Meg3"), class = "data.frame")

CodePudding user response:

Using dplyr

library(dplyr)
library(stringr)

df <- tibble::tribble(
  ~V1, ~C1, ~C10, ~C11, ~C12, ~C13, ~C14, ~C15, ~C16, ~C17, ~C18, ~C2, ~C3, ~C4, ~C5, ~C6, ~C7, ~C8, ~C9,
  "Mest",  1L,   1L,   1L,   1L,   0L,   0L,   0L,   0L,   0L,   0L,  0L,  0L,  0L,  0L,  0L,  0L,  0L,  0L,
  "Dlk1",  0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,  0L,  0L,  1L,  0L,  0L,  0L,  0L,  0L,
  "Meg3",  0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,   0L,  0L,  0L,  0L,  0L,  0L,  0L,  0L,  0L
)

select_order <- stringr::str_sort(names(df), numeric = TRUE)

df %>% 
  select(all_of(select_order))
#> # A tibble: 3 × 19
#>      C1    C2    C3    C4    C5    C6    C7    C8    C9   C10   C11   C12   C13
#>   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1     1     0     0     0     0     0     0     0     0     1     1     1     0
#> 2     0     0     0     1     0     0     0     0     0     0     0     0     0
#> 3     0     0     0     0     0     0     0     0     0     0     0     0     0
#> # … with 6 more variables: C14 <int>, C15 <int>, C16 <int>, C17 <int>,
#> #   C18 <int>, V1 <chr>
  •  Tags:  
  • r
  • Related