Merging rows based on values in two columns?

The following approach works for your example data. If in your full data each combination of id and year have only one numeric value for maytemp, juntemp and jultemp (like in the example data), the solution will work fine as well.

df<-data.frame(id,year,maytemp,juntemp,jultemp)%>%
  group_by(id,year)%>%
  summarise(maytemp=sum(maytemp,na.rm = T),
            juntemp=sum(juntemp,na.rm = T),
            jultemp=sum(jultemp,na.rm = T))
1 Like