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...