I have a df:
df= data.frame(year=c(rep(2018,4),rep(2017,3)),Area=c(1:4,1:3),P=1:7,N=1:7)
I want to split it by years, and then merge everything together again so I can see years as columns for each area. In order to do this, I am splitting and merging:
s=split(df,df$year)
m=merge(s[[1]][,2:4],[s[[2]][,2:4],by='Area',all=1)
colnames(m)=c('area','P2018','C2018','P2017','C2017')
I am sure there is a more efficient way, expecially as the possibility for errors is very high once I include data from other years.
Any suggestions?