This is my starting df
test <- data.frame(year = c(2018,2018,2018,2018,2018),
source = c("file1", "file1", "file1", "file1", "file1"),
area = c("000", "000", "800", "800", "800"),
cult2 = c("PBGEX", "QPGEX", "PBGEX", "QPGEX", "QPIND"),
value = c(1000,2000,3000,4000,5000))
year source area cult2 value
1 2018 file1 000 PBGEX 1000
2 2018 file1 000 QPGEX 2000
3 2018 file1 800 PBGEX 3000
4 2018 file1 800 QPGEX 4000
5 2018 file1 800 QPIND 5000
I need to get for each year/source/area the sum of value, for the fields PBGEX and QPGEX.
I was thinking of using spread
and gather
but I m losing many others columns (not show here).
This what I would except :
year source area cult2 value
1 2018 file1 000 PBGEX 1000
2 2018 file1 000 QPGEX 2000
3 2018 file1 800 PBGEX 3000
4 2018 file1 800 QPGEX 4000
5 2018 file1 800 QPIND 5000
6 2018 file1 000 RDGEX 3000
7 2018 file1 800 RDGEX 7000