3 ms·
The R package tidyr - a tidyverse package - makes this incredibly easy http://tidyr.tidyverse.org/ http://tidyr.tidyverse.org/. Just wanted to note that if yo
by alexilliamson 9y ago
The R package tidyr - a tidyverse package - makes this incredibly easy http://tidyr.tidyverse.org/ http://tidyr.tidyverse.org/. Just wanted to note that if you're considering trying out R and work with SQL, you will probably love the tidyverse. The SQL-like task replacements in tidyr, dplyr, and others, are how I came to love and appreciate R.
- autokad 9y agoi 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.
- makmanalp 9y agoFor pandas people: the pivot / pivot_table / melt / stack / unstack series of methods help with this stuff.
- spapas82 9y agoFor anybody finding pivot complex (I know I did), I've written a long post about pivot and friends: https://spapas.github.io/2016/09/21/pandas-pivot-table-primer/ https://spapas.github.io/2016/09/21/pandas-pivot-table-prime...