We can gather
data to long form excluding year
and Area
column, unite
the year
and then spread
it to wide format.
library(dplyr)
library(tidyr)
df %>%
gather(key, value, -year, -Area) %>%
unite(key, key, year, sep = "") %>%
spread(key, value)
# Area N2017 N2018 P2017 P2018
#1 1 5 1 5 1
#2 2 6 2 6 2
#3 3 7 3 7 3
#4 4 NA 4 NA 4