`which()` function for matrix indices
For your first question you need to also pass arr.ind= TRUE
to which
:
> which(m == 1, arr.ind = TRUE)
row col
[1,] 3 1
[2,] 2 2
[3,] 1 3
[4,] 4 3
You cannot mix numeric and alpha in a matrix, but you can in a data.frame:
> indices <- data.frame(ind= which(m == 1, arr.ind=TRUE))
> indices$rnm <- rownames(m)[indices$ind.row]
> indices$cnm <- colnames(m)[indices$ind.col]
> indices
ind.row ind.col rnm cnm
c 3 1 c e
b 2 2 b f
a 1 3 a g
d 4 3 d g