HOSS_JFL
HOSS_JFL

Reputation: 839

R: Converting a data frame of row/column indices to a matrix

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.

Upvotes: 2

Views: 2061

Answers (3)

jmcarter9t
jmcarter9t

Reputation: 823

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.

Upvotes: 0

bouncyball
bouncyball

Reputation: 10781

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.

Upvotes: 1

akrun
akrun

Reputation: 887891

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

Upvotes: 5

Related Questions