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!
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"
CodePudding user response:
We can use [
on order
ing 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"