I would like to sort a df like this one:
library(dplyr)
tags <- c("F23-F45", "A69-1008-1", "A69-1008-10", "A69-1008-10", "A69-1008-100", "A69-1008-12")
animal_names <- c("Dim","Dami", "Pet", "Nic", "Li", "Tan")
df <- tibble(tag = tags, animal_name = animal_names)
# A tibble: 6 x 2
tag animal_name
<chr> <chr>
1 F23-F45 Dim
2 A69-1008-1 Dami
3 A69-1008-10 Pet
4 A69-1008-10 Nic
5 A69-1008-100 Li
6 A69-1008-12 Tan
I would like to sort first by tag
and then by animal_name
. Typically I do it with arrange()
function of dplyr package. It would result in this df:
> df %>% arrange(tag, animal_name)
# A tibble: 6 x 2
tag animal_name
<chr> <chr>
1 A69-1008-1 Dami
2 A69-1008-10 Nic
3 A69-1008-10 Pet
4 A69-1008-100 Li
5 A69-1008-12 Tan
6 F23-F45 Dim
However, I would like to apply a kind of numeric sort so that A69-1008-12
occurrs before A69-1008-100
. This problem has been solved for a vector already (see this question) but I wonder how to transfer it to data.frames?