I have a logical matrix x with named rows (‘a’ and ‘b’) and named columns (’10’, ’20’, ’30’, ’40’). Let’s say, this:
10 20 30 40
a T F T F
b F T F T
structure(c(TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE),
.Dim = c(2L, 4L), .Dimnames = list(c("a", "b"), c("10", "20", "30", "40")))
Is there a short way to get a table that would list names of rows and columns where I have true values? That is, I want to get the following table:
a 10, 30
b 20, 40
Something similar can be obtained by which(x, arr.ind = T), which produces
row col
a 1 1
b 2 2
a 1 3
b 2 4
But I really want to get the first table.
You can directly use
apply.