0

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

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
voiDnyx
  • 119
  • 1
  • 2
  • 7

1 Answers1

2

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
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    Perhaps `sapply` in the second line? – Aaron left Stack Overflow Mar 01 '13 at 14:07
  • Yes thanks, works perfectly. Although I'd like to know if you can achieve the same with the reorder function? (reorder just seams to be predestined for this, the name and so) – voiDnyx Mar 01 '13 at 14:20
  • 1
    @voiDnyx I think `reorder` was designed for a different type of data type. You would have to unlist and assign a unique value, and then reorder. Sounds like reaching for your pocket by going behind your back (that phrase sounds way cooler in Slovene). – Roman Luštrik Mar 01 '13 at 14:31
  • 1
    To follow up on @Aaron's comment: try `unordered.median <- sapply(myList, median)` – Zach Mar 01 '13 at 14:50