Search code examples
rmedianranking

How to order list by median?


I'm quite new to R and having some problems understanding the reorder function.

Lets say i have a list with 3 vectors like:

myList <- (c(7,5,2),c(2,3,4),c(1,1,1))

and I want my list to be reordered by the median of each vector so that boxplotting the list gives me an ordered plot. Now how would I do this? I read the Help description for ?reorder but I cant seem to adapt the given example for my list.

any help would be appreciated


Solution

  • I think you want

    myList <- list(c(7,5,2),c(2,3,4),c(1,1,1))
    unordered.median <- unlist(lapply(myList, median))
    ordered.median <- order(unordered.median)
    
    myList[ordered.median]
    [[1]]
    [1] 1 1 1
    
    [[2]]
    [1] 2 3 4
    
    [[3]]
    [1] 7 5 2