Say I have a vector of countries and a vector of years
countries = c("Afganistan", "Armenia", ... "Zimbabwe")
year = 2004:2019
I want to create a list which repeats all the countries and adds a year for each repeat, like this:
Afghanistan 2004
Armenia 2004
...
Zimbabwe 2004
Afganistan 2005
Armenia 2005
...
Zimbabwe 2019
This kind of does it
x = data.frame(
countries = c("a", "b", "c", "d"),
years = c(1, 2, 3, 4),
years2 = c(1, 2, 3, 4)
)
melt(data = x, id.vars = "countries", measure.vars = c("years", "years2"))
But there must be an easier way so I don't have to repeat the years variable.