I start with data like this
and i want to get to this
I guess the procedure has to be similar to this solution for a similar question , but I can't modify it to get what I want.
Data
structure(list(...1 = c("Ciudad", "ciudad1", "ciudad2", "ciudad3",
"ciudad4"), `2000` = c("Hombres", "1", "2", "3", "4"), ...3 = c("Mujeres",
"5", "6", "7", "8"), `2001` = c("Hombres", "9", "10", "11", "12"
), ...5 = c("Mujeres", "13", "14", "15", "16"), `2002` = c("Hombres",
"17", "18", "19", "20"), ...7 = c("Mujeres", "21", "22", "23",
"24")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
Follow a solution:
another batch
tidyverse
The most complex part is "cleaning" the column names, basically we repeat the year values and combine them with the two gender values from the first row:
What remains is simpler: 1) we transform to a "long" format, 2) we divide the column into
Año
andSexo
3) we takeSexo
to the wide format.