R - reshape() and xtabs():transform columns to wide (matrix) format

02 Dec 2017

Transform a data frame in columns to wide matrix format

For a data frame that looks like

col_i   col_j   value
var1    varA    x
var2    varB    y
...     ...     ...

To transform into a matrix format

        varA        varB    ...
var1    x           ...
var2    ...          y
...

1) Using reshape

data_mat <- reshape(data_DT, idvar="col_i", timevar = "col_j", direction ="wide")
# but then the colnames are affected and should be updated !
#  colnames(data_mat) <- gsub("value, "", colnames(data_mat))

2) Using xtabs

data_mat <- as.data.frame.matrix(xtabs(value ~ col_i + col_j, data_DT))
# this does not affect column names
[ R  function  data frame  ]