0

I need to “pivot” a data.table. E.g., I need to go from this:

> head(patient_data)

         med     patient      mean_visits
1:        57     Alex         34.19
2:        57     Bob           2.30
3:       994     Alex          2.00
4:       998     Alex          5.33
5:      1764     Alex          9.77
6:      1764     Bob           1.00

to this:

> head(patient_data %>% spread(patient, mean_visits))

          med    Alex         Bob
 1:        57   34.19        2.30
 2:       994    2.00          NA
 3:       998    5.33          NA
 4:      1764    9.77        1.00

etc.

As you can see, for now I have a data.table object, which I am pivoting using tidyr's spread method, and it seems to work well, but I am wondering if there is a more efficient way, “native” to data.table to do that (and similar tidyr-flavored operations). Thanks...

Anarcho-Chossid
  • 2,210
  • 4
  • 27
  • 44
  • 3
    See `?data.table::dcast`. – joran Aug 10 '15 at 18:52
  • 2
    Try `dcast(patient_data, med~patient, value.var='mean_visits')` as @joran suggested – akrun Aug 10 '15 at 18:54
  • 1
    Check the *"Efficient reshaping using data.tables"* vignette in the [wiki](https://github.com/Rdatatable/data.table/wiki/Getting-started). – Arun Aug 10 '15 at 19:13
  • 1
    Thanks! (Why not give the answer as an answer? I’d ‘check’ it.) – Anarcho-Chossid Aug 10 '15 at 19:29
  • slides 19 and 20 in [this presentation](http://user2015.math.aau.dk/presentations/93.pdf) from useR 2015 are nice to visualise complex cases of reshaping. On that complex cases there is 9.41s vs 0.05s so you should get speedup switching to data.table – jangorecki Aug 10 '15 at 20:29

0 Answers0