简体   繁体   中英

Sort contingency table in R

I have a data frame (df)

structure(list(key = 1:10, x = structure(c(1L, 1L, 1L, 2L, 3L, 
4L, 5L, 5L, 5L, 5L), .Label = c("x1", "x2", "x3", "x4", "x5"), class = "factor"), 
    y = structure(c(2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("no", 
    "yes"), class = "factor")), .Names = c("key", "x", "y"), class = "data.frame", row.names = c(NA, -10L))

When I create a contingency table with table(df$x, df$y) I get this:

     no yes
  x1  1   2
  x2  0   1
  x3  1   0
  x4  1   0
  x5  1   3

But I would like sorted output on the yes column so output looks like so: no yes

    no  yes
  x5  1   3
  x1  1   2
  x2  0   1
  x3  1   0
  x4  1   0

I've been searching the web for a simple answer and I'm surprised that I cannot find any.

You can sort a table the same way you sort a matrix or a data.frame in R.

tt<-with(df, table(x,y))
tt[order(tt[,2], decreasing=T),]

#     y
# x    no yes
#   x5  1   3
#   x1  1   2
#   x2  0   1
#   x3  1   0
#   x4  1   0

Because I really like arrange from dplyr :

library(dplyr)
table(df$x, df$y) %>% as.data.frame.matrix(.) %>% arrange(desc(yes))

We have to do an intermediate step - convert the table to data frame - since dplyr doesn't know what to do with tables. This might be overkill for this problem, but arrange is a pretty useful little function. To me it's much easier to read and remember than using order .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM