Tengo un archivo excel de este estilo
structure(list(Territorio = c("Aguadulce", "Alanís", "Albaida del Aljarafe",
"Alcalá de Guadaíra", "Alcalá del Río", "Alcolea del Río",
"Algaba (La)", "Algámitas", "Almadén de la Plata"), `2007` = c(1,
2, 3, 4, 5, 6, 7, 8, 9), `2008` = c(10, 11, 12, 13, 14, 15, 16,
17, 18), `2009` = c(19, 20, 21, 22, 23, 24, 25, 26, 27), `2010` = c(28,
29, 30, 31, 32, 33, 34, 35, 36), `2011` = c(37, 38, 39, 40, 41,
42, 43, 44, 45), `2012` = c(46, 47, 48, 49, 50, 51, 52, 53, 54
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
))-> datos
Lo cambio a formato largo
library(tidyr)
dat_nuevos = gather(datos, key = Año, value = Valores, -Territorio)
pero ¿cómo lo hago cuando los datos son de este estilo?
structure(list(...1 = c("Territorio", "Aguadulce", "Alanís",
"Albaida del Aljarafe", "Alcalá de Guadaíra", "Alcalá del Río",
"Alcolea del Río", "Algaba (La)", "Algámitas", "Almadén de la Plata"
), Industria = c("2007", "1", "2", "3", "4", "5", "6", "7", "8",
"9"), ...3 = c("2008", "10", "11", "12", "13", "14", "15", "16",
"17", "18"), ...4 = c("2009", "19", "20", "21", "22", "23", "24",
"25", "26", "27"), Energia = c("2007", "28", "29", "30", "31",
"32", "33", "34", "35", "36"), ...6 = c("2008", "37", "38", "39",
"40", "41", "42", "43", "44", "45"), ...7 = c("2009", "46", "47",
"48", "49", "50", "51", "52", "53", "54")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
gather()
ha sido sustituida porpivot_longer()
. Entiendo que es preferible usar la segunda opción: tidyr.tidyverse.org/reference/gather.html