1
votes

Using R, I'm trying to construct a dataframe of the row and col numbers of a given matrix. E.g., if

a   <- matrix(c(1:15), nrow=5, ncol=3)

then I'm looking to construct a dataframe that gives:

row col
  1   1
  1   2
  1   3
  .   .
  5   1
  5   2
  5   3

What I've tried:

row <- matrix(row(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
col <- matrix(col(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
out <- cbind(row, col)
colnames(out) <- c("row", "col")

results in:

    row col
[1,]   1   1
[2,]   2   1
[3,]   3   1
[4,]   4   1
[5,]   5   1
[6,]   1   2
[7,]   2   2
[8,]   3   2
[9,]   4   2
[10,]  5   2
[11,]  1   3
[12,]  2   3
[13,]  3   3
[14,]  4   3
[15,]  5   3

Which isn't what I'm looking for, as the sequence of rows and cols in suddenly reversed, even tough I specified "byrow=T". I don't see if and where I'm making a mistake but would hugely appreciate suggestions to overcome this problem. Thanks in advance!

4

4 Answers

3
votes

Use row and col, but more directly manipulate their output ordering since they return corresponding indices in place for the input array. Use t to get the non-default order you want in the end:

data.frame(row = as.vector(t(row(a))), col = as.vector(t(col(a))))
    row col
 1    1   1
 2    1   2
 3    1   3
 4    2   1
 5    2   2
 6    2   3
 7    3   1
 8    3   2
 9    3   3
 10   4   1
 11   4   2
 12   4   3
 13   5   1
 14   5   2
 15   5   3

Or, as a matrix not a data.frame:

cbind(as.vector(t(row(a))), as.vector(t(col(a))))
  [,1] [,2]
 [1,]    1    1
 [2,]    1    2
 [3,]    1    3
 [4,]    2    1
 [5,]    2    2
 [6,]    2    3
 [7,]    3    1
 [8,]    3    2
 [9,]    3    3
 [10,]   4    1
 [11,]   4    2
 [12,]   4    3
 [13,]   5    1
 [14,]   5    2
 [15,]   5    3
3
votes

I'd use expand.grid on the vectors 1:ncol and 1:nrow, then flip the columns with [,2:1] to get them in the order you want:

> expand.grid(seq(ncol(a)),seq(nrow(a)))[,2:1]
   Var2 Var1
1     1    1
2     1    2
3     1    3
4     2    1
5     2    2
6     2    3
7     3    1
8     3    2
9     3    3
10    4    1
11    4    2
12    4    3
13    5    1
14    5    2
15    5    3
1
votes

You may want to have a look at ?expand.grid, which does just about exactly what you want to achieve.

0
votes

Since there are many ways to skin a cat, I'll chip in with yet another variant based on rep:

data.frame(row=rep(seq(nrow(a)), each=ncol(a)), col=rep(seq(ncol(a)), nrow(a)))

...but to announce a "winner", I think you need to time the solutions:

# Make up a huge matrix...
a <- matrix(runif(1e7), 1e4)

system.time( a1<-data.frame(row = as.vector(t(row(a))),
                            col = as.vector(t(col(a)))) ) # 0.68 secs

system.time( a2<-expand.grid(col = seq(ncol(a)),
                             row = seq(nrow(a)))[,2:1] ) # 0.49 secs

system.time( a3<-data.frame(row=rep(seq(nrow(a)), each=ncol(a)),
                            col=rep(seq(ncol(a)), nrow(a))) ) # 0.59 secs

identical(a1, a2) && identical(a1, a3) # TRUE

...so it seems @Spacedman has the speediest solution!