Say I have a list like:
my_list <- list(name1 = 'a', name2 = 'b', name3 = 'c', name4 = 'd')
And I want to order it according to the following variable:
my_var=c('name2','name1','name4','name3')
I tried the following, but does not work as I expect:
my_list[order(match(my_list, my_var))]
my_list[match(my_list, my_var)]
Any help? Thanks!
I guess you want to use match
like this
> my_list[match(my_var, names(my_list))]
$name2
[1] "b"
$name1
[1] "a"
$name4
[1] "d"
$name3
[1] "c"
BUT, you already can simply run my_list[my_var]
to achieve what you want.