I recently asked a question regarding database managemen in R packages tidyr, dplyr or similar in this link (Reorganizing columns by two column combination).
It was helpful, I managed to do the proposed code, but I was wring in the final format I was requiring.
I cannot manage to reach this format and was wondering how could be easily done.
Given this as an example database:
Factor 1 Factor 2 Year value1 value2
A green 2016 1.9 20
A green 2015 1.9 20
A green 2015 4 30
B yellow 2015 3 10
B yellow 2016 8 11
And trying to obtain:
Factor 1 Factor 2 value1.2015 value1.2016 value2.2015 value2.2016
A green 5.9 1.9 50 20
B yellow 3 8 10 11
So, it would be setting common identifiers for Factor, 1 and Factor 2, to spread the dataset by years and summing up common years for values 1 and 2
I am a begginer with tidyr, dplyr and cannot easily manage to do this.
I have been able to spread the dataset by years doing :
df.spread<-df %>%
gather(value1,value2,-factor1,-factor2,-Year) %>%
unite(Year,Year, value1, sep = "") %>%
spread(Year,value2)
but this does not sum the value1 for the common years as I want it.