Question

I want to arrange a list with characters based on order/arrange results on another list. For example, given a list char, and list of values (numbers) mini, I can get sorted char list:

sorted<-mapply(function(x, y) y[x], lapply(mini, order), char)

I want to use arrange/order that will sort char list based on ascending min list

(I want to have ascendant alphabetical char when values in mini are same).

Suggestions?

EDIT: dummy example

char <- list(A=c("dd", "aa", "cc", "ff"), B=c("rr", "ee", "tt", "aa"))
mini <- list(A=c(4,2,4,4), B=c(5,5,7,1))

char
$A
"dd" "aa" "cc" "ff" ...
$B
"rr" "ee" "tt" "aa" ...

mini
$A
4 2 4 4 ...
$B
5 5 7 1 ...

expected result:

sorted
$A
"aa" "cc" "dd" "ff"
$B
"aa" "ee" "rr" "tt"
Was it helpful?

Solution

Try this:

Map(function(x, y) y[order(x, y)], mini, char)

OTHER TIPS

lapply( names(char), function(nm) char[[nm]][order(mini[[nm]], char[[nm]])])
#------
[[1]]
[1] "aa" "cc" "dd" "ff"

[[2]]
[1] "aa" "ee" "rr" "tt"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top