1

I am working on a sales dataset and I need to restructure a data.table that looks like:

ID sales month
23 657   01
67 889   01
.
.
.
23 238   12

to the following format:

ID 01  02  03  ... 12
23 657 445 223 ... 238
67 889 990 765 ... 555
.
.
.

The code I currently have supplies a list of all unique IDs to lapply and the function gets all rows with the matching ID, creates a data table with one row, and joins it to the main restructured table using rbindlist.

This works for a small number of unique IDs (~600), but when the number of IDs is large, it's very slow and not feasible. Is there a more efficient way to do this? (A data.table specific approach?)

davey_j
  • 35
  • 4

1 Answers1

1

We can use dcast from data.table

library(data.table)
dcast(setDT(Df1), ID ~ month, value.var = 'sales')
akrun
  • 874,273
  • 37
  • 540
  • 662