0

I have a current R data.table that looks like this (toy example)

ID     Call    Sample
ID1      AA       S1
ID2      AB       S1
ID3      AA       S1
ID1      AB       S2
ID2      AB       S2
ID3      AA       S2

I'd like to create a new table from this one that (1) Groups by Sample, (2) Rearranges the table structure. Here's what I want the output to look like

Sample    ID1    ID2    ID3
  S1      AA     AB     AA
  S2      AB     AB     AA

I'm familiar with the super basics of dplyr and data.table, but can't seem to quite figure this out. If possible, I'd like to use these along with base R functions to do this transformation. However, I'm also willing to branch out to other packages if this can't be done with these.

Gaius Augustus
  • 940
  • 2
  • 15
  • 37
  • 3
    This is a standard "long" to "wide" reshape. Have you searched through the questions and answers already posted? – Pierre L Sep 25 '15 at 18:23
  • I couldn't find anything with my search parameters, but that was because I didn't know what it was called. I'll search around and mark this as duplicate if I find something. Thanks. – Gaius Augustus Sep 25 '15 at 18:26
  • 1
    Have a look at `?dcast`. – Jaap Sep 25 '15 at 18:28
  • 4
    See also the [vignette on reshaping with `data.table`](https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-reshape.html) – Jaap Sep 25 '15 at 18:35
  • Answer, using above suggestions: dcast(df, Sample ~ ID, value.var="Call") – Gaius Augustus Sep 25 '15 at 20:10

0 Answers0