There are two broad ways data can be stored in a matrix. In "wide" format, everything for a given subject / record is on a single line / row. For example:
patientId control_cells control_temp drugA_cells drugA_temp drugB_cells drugB_temp 1 ... 2 ... ...
Whereas the "long" format has multiple rows for a subject, one for each condition. For example:
patientId treatment cells temp 1 control ... 1 drugA ... 1 drugB ... 2 control ... ...
Wide is generally more useful than long. (It's a good rule of thumb to have a record per line, although what you consider to be a record may vary.) So how do you convert between the two? Easy. First, let's make a long matrix::
Note the row / subject id has to be a factor::
long_df$patientId <- factor (long_df$patientId)
And use "recast" from the "reshape2" library. The parameters are:
- the dataframe (don't know if this works with a matrix, may have to cast)
- a formula showing how the data is grouped, '...' means 'everything else'
This assumes that everything else is a measurement column. Columns will be named appropriately:
library (reshape2) wide_df <- recast (long_df, patientId ~ treatment + ...)
## Using patientId, treatment as id variables
## patientId control_temp control_cells drugA_temp drugA_cells drugB_temp ## 1 1 0.5693309 0.60104908 0.7737384 0.2919367 0.9171801 ## 2 2 0.1096891 0.98021730 0.2012493 0.9436605 0.8469883 ## 3 3 0.7531113 0.07349431 0.5581982 0.9701520 0.2299654 ## drugB_cells ## 1 0.3009366 ## 2 0.8923321 ## 3 0.8817151
print (colnames (wide_df))
##  "patientId" "control_temp" "control_cells" "drugA_temp" ##  "drugA_cells" "drugB_temp" "drugB_cells"
Now this is just the barebones of
recast and there are lots of options and possibilities. There are also several other libraries (notably
tidyr) that can also make this conversion and are arguably more powerful. However, to my eye they're much more complicated. For simple cases, the above works just fine. Note that many of the examples scattered across the web also assume there's only one measurement column.