4 ms·
i was looking into this last week. spread is what i found but i need to pivot on 2 keys, which I dont think is supported
by autokad 9y ago
i was looking into this last week. spread is what i found but i need to pivot on 2 keys, which I dont think is supported
- wodenokoto 9y agoI'm not sure exactly what you mean by 2 keys. Is it two key/value pairs or hierarchal 2 keys per value? If it is 2 keys per value, you can simply `unite()` the key columns. If it is two sets of key/values you simply spread twice. `df %>% spread(key1, val1) %>% spread(key2, val2)` tidy og dplyr are very much about composing things, so you do have to think a bit outside of the box of "which function" to use and rather "which functions" to use.
- extr 9y agoYou want the data.table [1] implementation of dcast and melt. They support multiple pivot values, inline aggregation, are super fast, and are easy to use compared to trying to wrap your brain around gather/spread. Frankly I think Hadley went a little too far with those tidyr functions, the dcast/melt interfaces he created in reshape2 are much, much easier to intuit and the DT versions are just improvements on that. [1] https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html https://cran.r-project.org/web/packages/data.table/vignettes...
- claytonjy 9y agoHuh, I feel the exact opposite here. melt/gather feel very similar, but spread seems like a massive improvement in UI over dcast. Used to be a big data.table fan, until I realized I could read my dplyr code as easily a week later as when I wrote it.