0

Please can anyone offer guidance on how to

  1. calculate column totals and then
  2. sort on the resultant totals in R?

Everything I've tried so far to total the columns, eg, (colsums() and sapply() returns the resultant totals as a vector (eg ABOUT 4022) and I cannot find any information on how I can split this into the Column Header ABOUT and Column Value 4022 and then sort both the Header and Value on the Column Value.

Vincent Malgrat
  • 66,725
  • 9
  • 119
  • 171
Julian
  • 1
  • doesn't `sort(colSums(mat))` with `mat` being your matrix or data frame do what you want? – user1981275 Apr 10 '13 at 13:50
  • Please make your situation reproducible, i.e. provide us with the data and the code needed to mimic your situation. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more tips on how to do this. – Paul Hiemstra Apr 10 '13 at 13:51

1 Answers1

3

Note that the function is called colSums (not colsums).

Use this with sort. Or if you want to order the columns use order:

colSums(mtcars)
sort(colSums(mtcars))

mtcars[ ,order(colSums(mtcars))]
EDi
  • 13,160
  • 2
  • 48
  • 57