Skip to content

Instantly share code, notes, and snippets.

@aurielfournier
Created February 2, 2016 22:27
Show Gist options
  • Save aurielfournier/bcca1f7952ebcb283260 to your computer and use it in GitHub Desktop.
Save aurielfournier/bcca1f7952ebcb283260 to your computer and use it in GitHub Desktop.
> head(amelt)
jdate year variable value
1 227 2013 pred_short 7.737878
2 247 2013 pred_short 38.029255
3 269 2013 pred_short 114.233338
4 237 2013 pred_short 64.805227
5 257 2013 pred_short 154.760291
6 284 2013 pred_short 88.092093
I want to have this
jdate 2013 2014 2015
227
247
...
filled with the median value for each combination .
can't spread() do that?
@bearloga
Copy link

bearloga commented Feb 2, 2016

I don't have the full dataset so I'm not sure how many combinations there are or if jdate is repeated, but try something like:

amelt %>%
  group_by(year, variable) %>%
  summarize(median = median(value)) %>%
  spread("year", "median")

Does that help?

@bearloga
Copy link

bearloga commented Feb 2, 2016

Forgot the preamble:

library(magrittr)
import::from(tidyr, spread)
import::from(dplyr, group_by, summarize)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment