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

Returning numbers assigned to names in descending order

I want to compute a function say ‘name_sort’ that takes the input names and values. The function should return the names from the second input – in decreasing order.

Thus values 3 2 1 should return the output A B and C

I am fairly new to R, so I am really struggling to wrap around my head if I am supposed to use an if statement or not. Any help would be much appreciated!

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

This is how I would like to start my statement:

names_sort <- function(values,names):

And this is the desired output

names_sort(c(3,1,2), c("A","B","C")) should return:

 [1] "A" "C" "B"

>Solution :

We can use [ on ordering of values in descending

names_sort <- function(values, names) {names[order(-values)]}

-testing

> names_sort(c(3,1,2), c("A","B","C"))
[1] "A" "C" "B"
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