Hi All,
I would like to sort my df by column that has got following levels or values:
Levels: P_1 P_2 P_3 P_4 P_5 P_6 P_7 P_8 P_9 P_10 P_11
when I use arrange it sorts like this:
that P_10 and P_11 are situated after P_1, but I want it to be after P_9.
How do I do it ? Please help.
FJCC
2
You can reset the levels of the column you want to sort.
library(dplyr)
NUMBERS <- sample(1:11)
DF <- data.frame(Fac = paste("P", NUMBERS, sep = "_"), stringsAsFactors = TRUE)
DF
#> Fac
#> 1 P_6
#> 2 P_4
#> 3 P_5
#> 4 P_7
#> 5 P_8
#> 6 P_10
#> 7 P_3
#> 8 P_9
#> 9 P_11
#> 10 P_1
#> 11 P_2
arrange(DF, Fac)
#> Fac
#> 1 P_1
#> 2 P_10
#> 3 P_11
#> 4 P_2
#> 5 P_3
#> 6 P_4
#> 7 P_5
#> 8 P_6
#> 9 P_7
#> 10 P_8
#> 11 P_9
DF <- DF %>% mutate(Fac = factor(Fac, levels = paste("P", 1:11, sep = "_")))
arrange(DF, Fac)
#> Fac
#> 1 P_1
#> 2 P_2
#> 3 P_3
#> 4 P_4
#> 5 P_5
#> 6 P_6
#> 7 P_7
#> 8 P_8
#> 9 P_9
#> 10 P_10
#> 11 P_11
Created on 2022-04-16 by the reprex package (v0.2.1)
Thank you very much indeed, really appreciated.
system
Closed
4
This topic was automatically closed 7 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.