pcantalupo - 3 months ago 43

R Question

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)`

`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`

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.

Answer

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
```