1

What is the most simple way of creating a data table from a data frame containing the information?

set.seed(1); atable <- data.frame(rows = c(rep("row1",3), rep("row2",3), rep("row3",3)), 

columns = c("col1", "col2", "col3"), values = runif(9))

> atable
  rows columns     values
1 row1    col1 0.96787365
2 row1    col2 0.53987921
3 row1    col3 0.48461553
4 row2    col1 0.42281684
5 row2    col2 0.34441145
6 row2    col3 0.06374566
7 row3    col1 0.44141985
8 row3    col2 0.39764457
9 row3    col3 0.56511001

The result should be a 3x3 table or matrix.

Thanks

Tiago Bruno
  • 413
  • 1
  • 3
  • 17
  • also https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Areza May 29 '21 at 20:36

1 Answers1

2

We can use xtabs

xtabs(values ~ rows + columns, atable)
akrun
  • 874,273
  • 37
  • 540
  • 662