Sorting dataframe by column of letters and numbers

I have been attempting to sort my dataframe by the first column - or day - with multiple different methods listed below to no avail. I suspect it could be because it is attempting to order by the first number but I am unsure how I would change that to get it to order the rows properly. The dataset is as follows:

df
    [day][sample1][sample2]
[1,]day0    22       11
[2,]day11   23       15
[3,]day15   25       14
[4,]day2    21       13
[5,]day8    20       17
...

I am looking to order the entire row by day. I have tried the following

df[sort(as.character(df$day)),]
df[order(as.character(df$day)),]
mixedorder(as.character(df$day))   (gtools package)

The mixedorder merely output an index of numbers.

1 answer

  • answered 2018-04-14 14:30 akrun

    The mixedorder gives the ordered index which can be used to order the rows

    df1 <- df[mixedorder(as.character(df$day)),]
    df1
    #     day sample1 sample2
    #1  day0      22      11
    #4  day2      21      13
    #5  day8      20      17
    #2 day11      23      15
    #3 day15      25      14