1

I have created a contingency table with several variables/cols by 510 categories/factors. I want to have factors ordered descending based on the sum of all variables/cols.

Tried converting table back to DF and rowSums but no luck. Not sure if possible to sort while using table function?

DF structure
'data.frame':   2210 obs. of  7 variables:
 $ Paddock_ID: num  1 1 1 1 1 1 1 1 1 1 ...
 $ Year      : num  2010 2011 2011 2012 2012 ...
 $ LandUse   : chr  "Wheat" "Wheat" "Wheat" "Wheat" ...
 $ LUT       : chr  "Cer" "Cer" "Cer" "Cer" ...
 $ LUG       : chr  "Wheat" "Wheat" "Wheat" "Wheat" ...
 $ Tmix      : Factor w/ 6 levels "6","5","4","3",..: 6 5 6 4 6 5 4 5 6 6 
...
 $ combo     : Factor w/ 510 levels "","GLYPHOSATE",..: 416 6 59 119 30 
22 510 2 2 509

my table
a <- table(DF$"combo", DF$"LUG") 

I get table ok but would like to have it ordered based on sum of all variables/columns i.e. Glyphosate = 124, then clethodim = 69, then paraquat = 53 ... descending for all 510 categories (rows).

                               Barley Canola Lupin Other Pasture Wheat

GLYPHOSATE                             4     46     6     5      23    40
TRALKOXYDIM                            0      0     0     0       0     8
MCPA; GLYPHOSATE; METSULFURON          0      0     0     0       0     1
METSULFURON                            1      0     0     0       0     1
BUTROXYDIM; METSULFURON                1      0     0     0       0     0
GLYPHOSATE; METSULFURON; PYRAFLUFEN    0      0     0     0       0     1
PARAQUAT                               2      7     7     2      28     7
CLETHODIM                              0     41    15     3       0     0
makeyourownmaker
  • 1,558
  • 2
  • 13
  • 33
mh1975
  • 13
  • 2

1 Answers1

0

Using an example dataset:

grades <- c(1,1,1,2,2,1,1,2,1,1,1,2,3)
credits <- c(4,4,4,8,4,4,8,4,4,4,8,4,4)
df <- cbind(grades, credits)

You can find the rowsums using rowSums().

One possible solution would be to create another column for rowsums and then sort with decreasing = T.

df <- as.data.frame(df)
df$sum <- rowSums(df)
df <- df[order(df[,3], decreasing = T),]
makeyourownmaker
  • 1,558
  • 2
  • 13
  • 33
fabla
  • 1,806
  • 1
  • 8
  • 20
  • Creating another column is unnecessary. – yarnabrina Jun 27 '19 at 06:39
  • 1
    Depending on whether or not u are interested in the actual sum. I´d assume when one is sorting depending on a value, they might be interested in keeping the information. – fabla Jun 27 '19 at 06:40
  • 1
    You can also put `order(df[,3])` in `rev()` for less typing. –  Jun 27 '19 at 07:13
  • 2
    @gersht I think `rev` will be less efficient than `decreasing = T`. Check description section of `?rev`. – yarnabrina Jun 28 '19 at 10:31
  • I have tried the rowSums option and decreasing =T but while the table shows as 6 cols in consol the structure is a list of 2 cols, shown below, so these don't work work 'table' int [1:507, 1:6] 0 0 5 0 0 0 1 1 1 0 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:507] "" "CLOPYRALID" "GLYPHOSATE" ..$ : chr [1:6] "Barley" "Canola" … Think I just need to rearrange this list so my crops are in separate cols then do rowsums and then decreasing but have tried levels and group_by with no luck? – mh1975 Jul 01 '19 at 05:33
  • Hi I, calculated Totals of Rows and placed new col at end of table using my_table <- cbind(my_table, Total = rowSums(my_table)). I now need to sort. have used this code newdata <- my_table[order(my_table$Total),] but get error "$ operator is invalid for atomic vectors" any tips please?? – mh1975 Jul 04 '19 at 04:37