There can never be too many examples for transforming data with R. So, here is another example of reshaping a `data.frame`

into a `matrix`

.

Here I have a data frame that shows incremental claim payments over time for different loss occurrence (origin) years.

The format of the data frame above is how this kind of data is usually stored in a data base. However, I would like to see the payments of the different origin years in rows of a matrix.

The first idea might be to use the `reshape`

function, but that would return a `data.frame`

. Yet, it is actually much easier with the `matrix`

function itself. Most of the code below is about formatting the dimension names of the matrix. Note that I use the `with`

function to save me a bit of typing.

An elegant alternative to `matrix`

provides the `acast`

function of the `reshape2`

package. It has a nice formula argument and allows me not only to specify the aggregation function, but also to add the margin totals.

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** mages' blog**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...