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?

5
  • give us some data to work on, and so, examples and so what you have done so far.
    – Ananta
    Sep 28, 2013 at 16:39
  • Welcome on SO. Please provide us a reproducible example.
    – sgibb
    Sep 28, 2013 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. Sep 28, 2013 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. Sep 28, 2013 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. Sep 28, 2013 at 17:10

2 Answers 2

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
0

Some users may want to sort the rows by decreasing total row frequencies and columns by decreasing total column frequencies. This would be the equivalent of setting order=freq when creating a two-way table in SAS using proc freq. This can be done in R as follows:

ExamSex <- table(Exam, Sex)
ExamSex <- ExamSex[order(rowSums(ExamSex), decreasing = TRUE), ]
ExamSex <- ExamSex[, order(colSums(ExamSex), decreasing = TRUE), ]

Or, in a more compact functional form with pipe operator (magrittr package required),

ordered_twoway <- function(x, y) {
 table(unname(x), unname(y)) %>%
     `[`(order(rowSums(.), decreasing = TRUE), ) %>%
     `[`(, order(colSums(.), decreasing = TRUE))
}

ordered_twoway(Exam, Sex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.