voiDnyx
voiDnyx

Reputation: 119

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

Upvotes: 0

Views: 718

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70643

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

Upvotes: 2

Related Questions