I want to sort a vector of strings based on the last character in R. Lets say I have m
m <- names(df)
m
[1] "test_1_p" "test_2_n" "test_3_p" "test_4_p" "test_5_n"
For reproduciblity:
m <- c("test_1_p","test_2_n","test_3_p","test_4_p","test_5_n")
Now my Desired output would be
[1] "test_2_n" "test_5_n" "test_1_p" "test_3_p" "test_4_p"
I tried to work with strsplit, but couldn't get it to work
r = strsplit(m,"_")
sorting_df = data.frame(r)
sort(sorting_df[3,])
I belief there has to be a fairly simple solution to that comparable to python where one could easily do it like this
lst = ["test_1_p","test_2_n","test_3_p","test_4_p","test_5_n"]
sorted(lst, key=lambda x: x[-1])
Edit All provided Solutions work thank you very much. Just accepted the first one.