0

I have a melted matrix A:

A =
1 1 1
2 1 0
2 2 1
3 1 0
3 2 0
3 3 1

I want to convert it to a B matrix:

B:
1  0  0
0  1  0
0  0   1

This matrix is symmetric.

We can easily melt a matrix using 'reshape' package. How can we do that inversely?

It is easy to use a for loop but it is too slow.

B <- matrix(0,nrow=3,ncol=3)

for(i in 1:nrow(A))
{
    B[A[i,1],A[i,2]] = A[i,3]
}
user1436187
  • 3,252
  • 3
  • 26
  • 59
  • Yours is a three-column matrix, not a "three-column data.frame" but I think you'll find an answer there or on one of the questions linked on the right side of that question. – Frank Oct 09 '13 at 03:53

1 Answers1

0
B <- matrix(0,nrow=3,ncol=3)
B[ A[, 1:2] ] <- A[,3]

We don't need no steenkin' loops here.

IRTFM
  • 258,963
  • 21
  • 364
  • 487