-2

Playing around with airquality, and I want to sort the data by Ozone and Wind (within wind)

I have the following command:

sort.airquality<-airquality[order(Ozone, Wind),]
sort.airquality[1:153,]

For some reason I just don't think this is right, I've tried looking up some of the tutorials but they don't seem to cover exactly what I'm looking for. Any help would be most appreciated.

Thanks.

killahtron
  • 595
  • 1
  • 4
  • 7

2 Answers2

2

You want

sort.airquality <- airquality[order(airquality$Ozone, airquality$Wind),]
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • Thank you. Does that make it within wind? Is it the last variable in the list that is the marker, or something like that? – killahtron Sep 01 '13 at 21:08
  • 2
    or using `with`: `airquality[with(airquality, order(c(Ozone, Wind))),]` – Jilber Urbina Sep 01 '13 at 21:09
  • 2
    @killahtron, just make a very, very simple data set and play around with yourself, and you will find out how the order of the ordering vectors matters. `df <- data.frame(x = c(4, 2, 2, 1, 3), y = runif(5))`, `df[order(df$x, df$y), ]`, `df[order(df$y, df$x), ]`, `df[order(-df$x, df$y), ]` – Henrik Sep 01 '13 at 21:21
  • 1
    @Jilber, don't you mean `order(Ozone, Wind)` instead of `order(c(Ozone, Wind))`? – Ferdinand.kraft Sep 01 '13 at 22:17
2

For improved readability, I suggest

sort.airquality <- airquality[with(airquality, order(Ozone, Wind)),]
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69