I have data of a lot of villages with unique identifiers (village), I am trying to shape the data into a long format by first expanding by year and then by month.
df_input <- data.frame( village=c("A","B","C"), population = c(1000,1500,2000))
df_output <- data.frame(year= c(2001,2001,2001,2002,2002,2002,2003,2003,2003),
month = c("1","2","3","1","2","3","1","2","3"),
village = c("A","A","A","B","B","B","C","C","C"),
population = c (1000, 1000,1000,1500,1500,1500,2000,2000,2000))
I am using the following code to first expan it by the year:
df_panel <- df_input %>%
merge(expand.grid(year=2001:2003, key=.$key), by="code_2011")
But I am getting the following error:
Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column
Any idea what I am doing wrong?