Search code examples
rsorting

Sort vector by given requirement


In mv , I want z at the first position ,the result as mv_sorted .How to handle it ? Thanks!

mv <- c('w','t','z','f')

mv_sorted <- c('z','w','t','f')

Solution

  • Try order

    > mv[order(mv != "z")]
    [1] "z" "w" "t" "f"
    
    
    > mv[order(mv == "z", decreasing = TRUE)]
    [1] "z" "w" "t" "f"