0

I created a table in R using the following command:

ExamSex<-table(Exam, Sex)

I want to now sort the table by values of Exam, and create a new table with sorted values. How can I do that?

sgibb
  • 25,396
  • 3
  • 68
  • 74
  • give us some data to work on, and so, examples and so what you have done so far. – Ananta Sep 28 '13 at 16:39
  • Welcome on SO. Please provide us a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – sgibb Sep 28 '13 at 16:40
  • Perhaps `ExamSex[order(rownames(ExamSex)), ]`? By default, I would expect the values of "Exam" to end up as your `rownames`, but as others have suggested, show some sample data, what output you expect, and what you've tried. – A5C1D2H2I1M1N2O1R2T1 Sep 28 '13 at 16:42
  • Hey Ananda - thanks a ton... so i was missing the "," in subsetting ExamSex and it kept returning values from the table but did not present the table as normal. Learning R a bit on the fly so my fundamentals are a bit challenged. Thanks again. – user2826690 Sep 28 '13 at 16:55
  • @user2826690, OK. I've posted it as an answer for the benefit of others. In the future, try to post a reproducible example so others aren't guessing at what you want to do. – A5C1D2H2I1M1N2O1R2T1 Sep 28 '13 at 17:10

1 Answers1

2

A table is essentially a matrix, so you can work with it in much the same way you would work with a matrix.

When you use table on two items, the rownames become the unique values of the first item and the colnames become the unique values of the second item.

Here's an example:

out <- table(state.division, state.region)
out
#                     state.region
# state.division       Northeast South North Central West
#   New England                6     0             0    0
#   Middle Atlantic            3     0             0    0
#   South Atlantic             0     8             0    0
#   East South Central         0     4             0    0
#   West South Central         0     4             0    0
#   East North Central         0     0             5    0
#   West North Central         0     0             7    0
#   Mountain                   0     0             0    8
#   Pacific                    0     0             0    5
unique(state.division)
# [1] East South Central Pacific            Mountain           West South Central
# [5] New England        South Atlantic     East North Central West North Central
# [9] Middle Atlantic   
# 9 Levels: New England Middle Atlantic South Atlantic ... Pacific
unique(state.region)
# [1] South         West          Northeast     North Central
# Levels: Northeast South North Central West

Thus, if we wanted to order by the first value, we would need to order by the rownames of your table:

out[order(rownames(out)), ]
#                     state.region
# state.division       Northeast South North Central West
#   East North Central         0     0             5    0
#   East South Central         0     4             0    0
#   Middle Atlantic            3     0             0    0
#   Mountain                   0     0             0    8
#   New England                6     0             0    0
#   Pacific                    0     0             0    5
#   South Atlantic             0     8             0    0
#   West North Central         0     0             7    0
#   West South Central         0     4             0    0
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485