-2

I have this table:

head(tb)
    shot_made_flag
opponent        0        1
    ATL 54.79452 45.20548
    BKN 60.00000 40.00000
    BOS 58.87612 41.12388
    CHA 56.40000 43.60000
    CHI 56.97674 43.02326
    CLE 56.03113 43.96887

And this is my code:

table(opponent,shot_made_flag)
rowSums(table(opponent,shot_made_flag))
b<table(opponent,shot_made_flag)/rowSums(table(opponent,shot_made_flag))*100
head(tb)

I would like to sort my table by values in column 1, but I never get the table back as a result. I only get the values in column 1 sorted in crescent (or decrescent order)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Claudio
  • 63
  • 1
  • 1
  • 7
  • Please use `dput` to show the `data`. Is it a `table` object? – akrun Jul 29 '16 at 09:41
  • Can you show the code you used to execute the MySQL query? What package are you using? – Tim Biegeleisen Jul 29 '16 at 09:43
  • Can you share your code? – vanathaiyan Jul 29 '16 at 09:50
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jul 29 '16 at 10:03

1 Answers1

0

Use order function and a - (minus) sign before the column to be arranged in descending order and no minus sign if you want the Result in ascending order.

   df <- read.table(header=T, text="
   opponent        0        1
   ATL 54.79452 45.20548
   BKN 60.00000 40.00000
   BOS 58.87612 41.12388
   CHA 56.40000 43.60000
   CHI 56.97674 43.02326
   CLE 56.03113 43.96887
   ",check.names=F)

   df
   #   opponent        0        1
   # 1      ATL 54.79452 45.20548
   # 2      BKN 60.00000 40.00000
   # 3      BOS 58.87612 41.12388
   # 4      CHA 56.40000 43.60000
   # 5      CHI 56.97674 43.02326
   # 6      CLE 56.03113 43.96887

   names(df)
   # [1] "opponent"  "0"       "1"      

   Result <- df[order(-df$"1"),]
   Result
   #   opponent        0        1
   # 1      ATL 54.79452 45.20548
   # 6      CLE 56.03113 43.96887
   # 4      CHA 56.40000 43.60000
   # 5      CHI 56.97674 43.02326
   # 3      BOS 58.87612 41.12388
   # 2      BKN 60.00000 40.00000

   # Alternative
   Result <- df[order(-df[,3]),]
   Result
   #   opponent        0        1
   # 1      ATL 54.79452 45.20548
   # 6      CLE 56.03113 43.96887
   # 4      CHA 56.40000 43.60000
   # 5      CHI 56.97674 43.02326
   # 3      BOS 58.87612 41.12388
   # 2      BKN 60.00000 40.00000
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30