Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort values of named vector A by vector B but keeping the order of names where possible

I have a named vector like that:

my_vec <- c("Max"= "England", "Manfred"= "Germany", "Ingolf"= "Germany", "Paul"= "England", "Peter"= "Germany", "Nina"= "Italy")

I need to sort this vector by countries as specified in another vector while keeping the order of the persons for a given country as before. So if the ordering of the countries is…

sort_scheme <- c("England", "Germany", "Italy")

… then my desired output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

goal_vec <- c("Max"= "England", "Paul"= "England", "Manfred"= "Germany", "Ingolf"= "Germany", "Peter"= "Germany", "Nina"= "Italy")

As you can see the desired output has the same order of countries as in sort_scheme (England first, then Germany, then Italy) and, inside one country the original order of names is the same (for England Max comes first then Paul, and so on). The solution should be flexible for other characters (new names and countries) with base R. All I’ve come up with is incredibly cumbersome.

Edit I found this being a duplicate and voted to close.

>Solution :

We can use order:

my_vec[order(match(my_vec, sort_scheme))]

      Max      Paul   Manfred    Ingolf     Peter      Nina 
"England" "England" "Germany" "Germany" "Germany"   "Italy" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading