how to arrange data in desired order

I have a data frame as below, and want to sort by id.
the sorted order is 'CKK-01', 'CKK-02', 'TTK-1','TTK-10','TTK-2'
how to make: 'CKK-01', 'CKK-02', 'TTK-1','TTK-2','TTK-10'?

employee <- c('John Doe','Peter Gynn','Jolie Hope','Linda White','new')
id <- c('CKK-01', 'CKK-02', 'TTK-1','TTK-2','TTK-10')
ds <- data.frame(employee, id)
ds <- ds %>% arrange(id)

Hi @tjcnnl1,

When sorting by characters, since 1 is considered coming before any following number, 10 comes before 2, for example. One way to get around this would be to split the ID into its two components, the character prefix, and numeric suffix, and sort by each of those:

library(tidyverse)
employee <- c('John Doe','Peter Gynn','Jolie Hope','Linda White','new')
id <- c('CKK-01', 'CKK-02', 'TTK-1','TTK-2','TTK-10')
ds <- data.frame(employee, id)
ds %>% 
  separate(id, into = c("prefix", "suffux"), sep = "-", remove = FALSE, 
           convert = TRUE) %>% 
  arrange(prefix, suffux)
#>      employee     id prefix suffux
#> 1    John Doe CKK-01    CKK      1
#> 2  Peter Gynn CKK-02    CKK      2
#> 3  Jolie Hope  TTK-1    TTK      1
#> 4 Linda White  TTK-2    TTK      2
#> 5         new TTK-10    TTK     10

Created on 2020-11-16 by the reprex package (v0.3.0)

1 Like

Perfect! Thank you so much.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.