It probably easier explain myself using an example.
Let’s say I have a list s:
s <- list( c(5,3,4,3,6),c(“A”,”B”,”C”,”D”,”E”))
s has always the same number of object for all sub-vectors. NA value are not allowed. The vectors contain different types.
What I want to achieve is:
rank v1 v2
1 3 "B"
2 3 "D"
3 4 "C"
4 5 "A"
5 6 "E"
Basically, to sort the list based on the first vector (in ascending order) and then (in case on tie) look to the second vector using the lexicological order. In C++ world the only thing that I need to do is to define the operator< for my object, however I am pretty new of R and I am running out ideas.
The best strategy that I have found is to loop over the elements and calculate a rank value (double) for each couple (eg. 3 “B” will result with the highest rank and 6 “E” with the lowest), store the results in another vector and sort it. However the solution is not great because find a good ranking function can be tricky and it is not very easy to generalize.
It seems to me such a common problem that it has to be a better way. Can anyone point me in the right direction?
Thanks for your help.
Use
order():Note that I transformed your list to a data frame. Since a data frame is itself a list (with all elements the same length) this shouldn’t be a problem in your case.