g.f.l g.f.l - 3 months ago 17
R Question

Sort data frame by numbers

I have a data frame with about 40 columns 2000 rows. One of them has numbers and letters like this :

CLN

5T
14S
1A
12N


At first I used the
melt
in order to get a long structure of my data. I want to plot my data with the CLN at the yaxis but with the order of numbers like : 1A,5T,12N,14S. I know that I have to use the
sort
but how can I do this for my numbers and letters? (I am sorry but my df is huge and I cannot provide a productive example).

Answer

Try mixedsort from gtools:

vec <- c("5T", "14S", "1A", "12N")

gtools::mixedsort(vec)
# [1] "1A"  "5T"  "12N" "14S"