2

I've used LimeSurvey to capture ranking information for 8 options across a large class of students. I've exported the results as CSV, and I get something like the following...

Team1 Option7 Option4 Option6 Option1 Option2 Option3 Option5 Option8
Team2 Option4 Option3 Option1 Option2 Option7 Option8 Option6 Option5
...

... where the ranking goes from lowest to highest (e.g. Team1 most preferred Option7). What I'm trying to turn this into is something like...

Option1 Option2 Option3 Option4 Option5 Option6 Option7 Option8
4       5       6       2       7       3       1       8
3       4       2       1       8       7       5       6
...

... in as "R like" a way as possible (e.g. avoiding nested for loops and the like). My end goal is to generate histograms for each option showing how often they were voted first, second, etc. I have a suspicion that reshape and it's ilk might help, but having to use the "position" information is tripping me up.

Any assistance gratefully appreciated!

Jason Foster
  • 81
  • 1
  • 2

1 Answers1

1
 dat <- read.table(text="Team1 Option7 Option4 Option6 Option1 Option2 Option3 Option5 Option8
     Team2 Option4 Option3 Option1 Option2 Option7 Option8 Option6 Option5")

 cbind( dat[,1,drop=FALSE ], t( apply(dat[ , -1], 1, order) ) )
#     V1 1 2 3 4 5 6 7 8
#1 Team1 4 5 6 2 7 3 1 8
#2 Team2 3 4 2 1 8 7 5 6

The drop =FALSE is needed to prevent the column from becoming a vector. The transpose is needed because apply returns a matrix in column major order.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Looks perfect; thanks! From your answer I've learned about `order`, the `-1` index, the `drop` parameter, `cbind`, and the basic `apply` (as opposed to the more commonly seen `sapply`)... an awesome information density! – Jason Foster Mar 08 '12 at 14:00