1

I have a mock up data set:

d1 = structure(
  list(
    chan1 = c(1.49955768204777, 1.57924608282282, 
              1.62079872172079, 1.49955768204777,
              1.50897108417039, 1.47897959168283),
    chan2 = c(3.71459266186863, 3.71459266186863,
              3.66763591782946, 3.67359273988532,
              3.66408366995924, 3.68083665073346),
    chan3 = c(8.32529316285155, 6.30229174858652,
              6.97551768293611, 6.52653674461786,
              6.52653674461786, 6.07823977152575),
    chan4 = c(11.023719681933, 11.023719681933, 
              11.023719681933, 11.4613297390623,
              11.4613297390623, 11.5813471428122),
    chan5 = c(7.32862391337389, 7.38103675023449,
              7.81796038841145, 7.4216715642288,
              7.51924428352424, 7.35498863975821),
    rowname = c(2042051, 1454646, 289170,
                3307469, 3890829, 1741489),
    total_conv = c(359.161333500186, 359.161312264452,
                   359.16130836516, 359.161294408793,
                   359.161289598969, 359.161209958641),
    sum = c(31.8917871020749, 30.0008869254455, 
            31.1056323928309, 30.5826884698421,
            30.6801655213341, 30.1743917965125)
  ),
  .Names = c("chan1", "chan2", "chan3", "chan4", "chan5",
             "rowname", "total_conv", "sum"),
  class = "data.frame",
  row.names = c(NA, -6L)
)

Now I need to sort this data set by total_conv and sum variables. Here total_conv should be sort in descending order and sum in ascending order.

When I use the following function, I unable to sort my data set in required format.

d1<-setorder(as.data.table(d1),-total_conv,sum)

How can I overcome this issue?

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
user3747924
  • 11
  • 1
  • 3
  • 1
    You seem to miss the whole point of `setorder()`. Please have a look at [this answer](http://stackoverflow.com/a/29331287/559784). With `1.9.5`, `setorder()` works on data.frames too. – Arun Apr 24 '15 at 13:05

1 Answers1

2

You can also try order instead of setorder:

setDT(d1)[order(-total_conv, sum)]

It will first sort by descending total_conv and then by descending sum.

Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
  • 1
    For some reason I can't use setorder (package upgrade is a pain at my work...not instantaneous), reason of my reply. But `setorder(setDT(d1), -total_conv, sum)` should indeed work. – Colonel Beauvel Apr 24 '15 at 13:11
  • If you already worked in some environments, downloading is blocked on lots of dev depository/websites ;) I know this solution but can't apply it unfortunately! – Colonel Beauvel Apr 24 '15 at 13:13