2

I would like to transform an object like this

df <- data.frame(ROW = c(1,3),COLUMN =c(2,3),VALUE = c(10,20))
df
ROW COLUMN VALUE 
1        2    10
3        3    20

to a matrix like this

m <-matrix(c(0,10,0,0,0,0,0,0,20),ncol = 3,nrow = 3)
m
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]   10    0    0
[3,]    0    0   20

I know that the data frame represents a sparse matrix but I did not find any other question that relates exactly to what I am looking for.

HOSS_JFL
  • 765
  • 2
  • 9
  • 24
  • Possible duplicate of [Reshape three column data frame to matrix ("long" to "wide" format)](https://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix-long-to-wide-format) – divibisan Mar 20 '19 at 14:08

3 Answers3

5

We can use sparseMatrix

library(Matrix)
as.matrix( sparseMatrix(i = df$COLUMN, j= df$ROW, x= df$VALUE))
#      [,1] [,2] [,3]
#[1,]    0    0    0
#[2,]   10    0    0
#[3,]    0    0   20

Or create a matrix of 0's and then assign

m1 <- matrix(0, 3, 3)
m1[as.matrix(df[2:1])] <- df$VALUE

Note: Based on the output, the ROW/COLUMN index seems to be reversed

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    `replace(matrix(0, max(df$ROW), max(df$COLUMN)), as.matrix(df[c("ROW", "COLUMN")]), df$VALUE)` – d.b Oct 31 '17 at 16:20
1

We can iterate over the rows of df and fill a matrix according to the row and column indexes included in df:

# initialize
new_mat <- matrix(0, ncol = max(df$ROW), nrow = max(df$COLUMN))

for(i in 1:nrow(df)){
  new_mat[df[i,]$COLUMN, df[i,]$ROW] <- df[i,]$VALUE
}

#      [,1] [,2] [,3]
# [1,]    0    0    0
# [2,]   10    0    0
# [3,]    0    0   20

as @akrun has noted, it seems as though the row and column indexes are flipped.

bouncyball
  • 10,631
  • 19
  • 31
0

Here is a solution with apply

mat <- matrix( 0, ncol = max(df$ROW), nrow = max(df$COLUMN) )
f <- function( x ) { mat[x[1],x[2]] <<- x[3] }
apply( df, 1, f } )

The <<- operator applies the value to the externally defined mat. May need to use as.numeric etc. for data type conversions.

jmcarter9t
  • 731
  • 6
  • 11