Have a dataframe:
structure(list(Cod = c("007", "009", "011", "014", "016"), Municipio = c("Huelmo",
"Busdongo", "Duero", "Coin", "Anduva"), A1 = c("PSOE", "PP",
"PSOE", "PSOE", "PSOE"), A2 = c("00000248", "00000044", "00000089",
"00001637", "00000148"), A3 = c("05030", "03928", "08240", "03968",
"05016"), B1 = c("PP", "PSOE", "VOX", "PP", "PP"), B2 = c("00000072",
"00000033", "00000002", "00000924", "00000077"), B3 = c("01460",
"02946", "00185", "02240", "02610"), C1 = c("PODEMOS", "PODEMOS",
"PP", "Cs", "Cs"), C2 = c("00000066", "00000020", "00000002",
"00000386", "00000018"), C3 = c("01338", "01785", "00185", "00935",
"00610"), D1 = c("VOX", "VOX", "Cs", "PODEMOS", "VOX"), D2 = c("00000041",
"00000004", "00000002", "00000296", "00000015"), D3 = c("00831",
"00357", "00185", "00717", "00508"), E1 = c("Cs", "Cs", "CEUS",
"CEUS", "PODEMOS"), E2 = c("00000021", "00000003", "00000001",
"00000247", "00000015"), E3 = c("00425", "00267", "00092", "00598",
"00508"), F1 = c("PH", "CEUS", "AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)",
"AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)", "FE de las JONS"
), F2 = c("00000009", "00000002", "00000001", "00000146", "00000001"
), F3 = c("00182", "00178", "00092", "00353", "00033"), G1 = c("RECORTES CERO",
"AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)", "PCPE/PCPC",
"VOX", "IZQESP"), G2 = c("00000006", "00000001", "00000001",
"00000107", "00000000"), G3 = c("00121", "00089", "00092", "00259",
"00000"), H1 = c("JUNTS-UE", "PCTE", "PACMA", "JUNTS-UE", "CEUS"
), H2 = c("00000003", "00000001", "00000001", "00000058", "00000000"
), H3 = c("00060", "00089", "00092", "00140", "00000"), I1 = c("CEUS",
"IZQESP", "FE de las JONS", "ANDALUCISTAS", "FO"), I2 = c("00000002",
"00000000", "00000001", "00000045", "00000000"), I3 = c("00040",
"00000", "00092", "00109", "00000"), J1 = c("AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)",
"FO", "PH", "IZQP - UNIDOS - DEf", "AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)"
), J2 = c("00000002", "00000000", "00000001", "00000028", "00000000"
), J3 = c("00040", "00000", "00092", "00067", "00000"), K1 = c("PIRATES.CAT / REBELDES",
"JUNTS-UE", "RECORTES CERO", "PCPE/PCPC", "JUNTS-UE"), K2 = c("00000002",
"00000000", "00000001", "00000025", "00000000"), K3 = c("00040",
"00000", "00092", "00060", "00000"), L1 = c("PACMA", "ANDALUCISTAS",
"IZQESP", "PH", "ANDALUCISTAS"), L2 = c("00000001", "00000000",
"00000000", "00000016", "00000000"), L3 = c("00020", "00000",
"00000", "00038", "00000"), M1 = c("IZQESP", "IE", "FO", "VOLT",
"IE"), M2 = c("00000000", "00000000", "00000000", "00000016",
"00000000"), M3 = c("00000", "00000", "00000", "00038", "00000"
), N1 = c("FO", "ESCAĂ‘OS EN BLANCO", "JUNTS-UE", "FE de las JONS",
"ESCAĂ‘OS EN BLANCO"), N2 = c("00000000", "00000000", "00000000",
"00000015", "00000000"), N3 = c("00000", "00000", "00000", "00036",
"00000"), O1 = c("ANDALUCISTAS", "CRT", "ANDALUCISTAS", "EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX",
"CRT"), O2 = c("00000000", "00000000", "00000000", "00000013",
"00000000"), O3 = c("00000", "00000", "00000", "00031", "00000"
), P1 = c("IE", "PFAC", "IE", "FO", "PFAC"), P2 = c("00000000",
"00000000", "00000000", "00000011", "00000000"), P3 = c("00000",
"00000", "00000", "00026", "00000"), Q1 = c("ESCAĂ‘OS EN BLANCO",
"PCPE/PCPC", "ESCAĂ‘OS EN BLANCO", "PCTE", "PCTE"), Q2 = c("00000000",
"00000000", "00000000", "00000009", "00000000"), Q3 = c("00000",
"00000", "00000", "00021", "00000"), R1 = c("CRT", "SALAMANCA-ZAMORA-LEĂ“N PREPAL",
"CRT", "RECORTES CERO", "PCPE/PCPC"), R2 = c("00000000", "00000000",
"00000000", "00000006", "00000000"), R3 = c("00000", "00000",
"00000", "00014", "00000"), S1 = c("PFAC", "P-LIB", "PODEMOS",
"PIRATES.CAT / REBELDES", "SALAMANCA-ZAMORA-LEĂ“N PREPAL"), S2 = c("00000000",
"00000000", "00000000", "00000006", "00000000"), S3 = c("00000",
"00000", "00000", "00014", "00000"), T1 = c("PCTE", "SUMAR",
"PFAC", "IZQESP", "P-LIB"), T2 = c("00000000", "00000000", "00000000",
"00000004", "00000000"), T3 = c("00000", "00000", "00000", "00009",
"00000"), U1 = c("PCPE/PCPC", "PACMA", "PCTE", "IE", "SUMAR"),
U2 = c("00000000", "00000000", "00000000", "00000003", "00000000"
), U3 = c("00000", "00000", "00000", "00007", "00000"), V1 = c("SALAMANCA-ZAMORA-LEĂ“N PREPAL",
"CREE EN EUROPA", "SALAMANCA-ZAMORA-LEĂ“N PREPAL", "PACMA",
"PACMA"), V2 = c("00000000", "00000000", "00000000", "00000003",
"00000000"), V3 = c("00000", "00000", "00000", "00007", "00000"
), W1 = c("P-LIB", "PMR", "P-LIB", "ESCAĂ‘OS EN BLANCO",
"CREE EN EUROPA"), W2 = c("00000000", "00000000", "00000000",
"00000000", "00000000"), W3 = c("00000", "00000", "00000",
"00000", "00000"), X1 = c("SUMAR", "S.A.L.F", "SUMAR", "CRT",
"PMR"), X2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), X3 = c("00000", "00000", "00000", "00000", "00000"
), Y1 = c("CREE EN EUROPA", "F", "CREE EN EUROPA", "PFAC",
"S.A.L.F"), Y2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), Y3 = c("00000", "00000", "00000", "00000", "00000"
), Z1 = c("PMR", "EXISTE", "PMR", "SALAMANCA-ZAMORA-LEĂ“N PREPAL",
"F"), Z2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), Z3 = c("00000", "00000", "00000", "00000", "00000"
), AA1 = c("S.A.L.F", "FE de las JONS", "S.A.L.F", "P-LIB",
"EXISTE"), AA2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), AA3 = c("00000", "00000", "00000", "00000",
"00000"), AB1 = c("F", "SAE", "F", "SUMAR", "SAE"), AB2 = c("00000000",
"00000000", "00000000", "00000000", "00000000"), AB3 = c("00000",
"00000", "00000", "00000", "00000"), AC1 = c("EXISTE", "J.U.E.X.",
"EXISTE", "CREE EN EUROPA", "J.U.E.X."), AC2 = c("00000000",
"00000000", "00000000", "00000000", "00000000"), AC3 = c("00000",
"00000", "00000", "00000", "00000"), AD1 = c("FE de las JONS",
"PH", "SAE", "PMR", "PH"), AD2 = c("00000000", "00000000",
"00000000", "00000000", "00000000"), AD3 = c("00000", "00000",
"00000", "00000", "00000"), AE1 = c("SAE", "VOLT", "J.U.E.X.",
"S.A.L.F", "VOLT"), AE2 = c("00000000", "00000000", "00000000",
"00000000", "00000000"), AE3 = c("00000", "00000", "00000",
"00000", "00000"), AF1 = c("J.U.E.X.", "RECORTES CERO", "VOLT",
"F", "RECORTES CERO"), AF2 = c("00000000", "00000000", "00000000",
"00000000", "00000000"), AF3 = c("00000", "00000", "00000",
"00000", "00000"), AG1 = c("VOLT", "PAZ SOBERANĂŤA REPĂšBLICA",
"PAZ SOBERANĂŤA REPĂšBLICA", "EXISTE", "PAZ SOBERANĂŤA REPĂšBLICA"
), AG2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), AG3 = c("00000", "00000", "00000", "00000",
"00000"), AH1 = c("PAZ SOBERANĂŤA REPĂšBLICA", "FIEL", "FIEL",
"SAE", "FIEL"), AH2 = c("00000000", "00000000", "00000000",
"00000000", "00000000"), AH3 = c("00000", "00000", "00000",
"00000", "00000"), AI1 = c("FIEL", "IZQP - UNIDOS - DEf",
"IZQP - UNIDOS - DEf", "J.U.E.X.", "IZQP - UNIDOS - DEf"),
AI2 = c("00000000", "00000000", "00000000", "00000000", "00000000"
), AI3 = c("00000", "00000", "00000", "00000", "00000"),
AJ1 = c("IZQP - UNIDOS - DEf", "EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX",
"EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX", "PAZ SOBERANĂŤA REPĂšBLICA",
"EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX"), AJ2 = c("00000000",
"00000000", "00000000", "00000000", "00000000"), AJ3 = c("00000",
"00000", "00000", "00000", "00000"), AK1 = c("EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX",
"glg", "glg", "FIEL", "glg"), AK2 = c("00000000", "00000000",
"00000000", "00000000", "00000000"), AK3 = c("00000", "00000",
"00000", "00000", "00000"), AL1 = c("glg", "PIRATES.CAT / REBELDES",
"PIRATES.CAT / REBELDES", "glg", "PIRATES.CAT / REBELDES"
), AL2 = c("00000000", "00000000", "00000000", "00000000",
"00000000"), AL3 = c("00000", "00000", "00000", "00000",
"00000"), AM1 = c("NEX", "NEX", "NEX", "NEX", "NEX"), AM2 = c("00000000",
"00000000", "00000000", "00000000", "00000000"), AM3 = c("00000",
"00000", "00000", "00000", "00000")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame"))
First, I transform the numbers that are in character format to numeric format.
col_voto <- c("Cod","A2","A3","B2","B3","C2","C3","D2","D3","E2","E3","F2","F3","G2","G3","H2","H3","I2","I3","J2","J3","K2","K3","L2","L3","M2","M3","N2","N3","O2","O3",
"P2","P3","Q2","Q3","R2","R3","S2","S3","T2","T3","U2","U3","V2","V3","W2","W3","X2","X3","Y2","Y3","Z2","Z3","AA2","AA3","AB2","AB3","AC2","AC3","AD2","AD3",
"AE2","AE3","AF2","AF3","AG2","AG3","AH2","AH3","AI2","AI3","AJ2","AJ3","AK2","AK3","AL2","AL3","AM2","AM3")
and then
df <- df %>%
mutate(across(all_of(col_voto), as.numeric))
Then create and apply a function to isolate each section of the data.
stack_data = function(i) {
d = select(europeas, Municipio, contains(i), -Cod)
names(d) = str_replace(names(d), i, 'col')
d
}
and map through "sections" A-AM, stack, and reshape
df <- map_df(c('A', 'B','C', 'D', 'E', 'F', 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG',
'AH','AI','AJ','AK','AL','AM'), stack_data) |>
filter(!is.na(col1)) |>
pivot_longer(c(-'Municipio', -'col1')) |>
pivot_wider(names_from = c(col1, name), values_from = value) |>
select(Municipio, contains('PSOE'), contains('PP'), contains('IZQESP'), contains('CEUS'), contains('EL PI'), contains('CCa'), contains('GBAI'),
contains('EAJ-PNV'), contains('FO'), contains('AHORA REPÚBLICAS (ERC-EH BILDU-BNG-ARA MÉS)'), contains('JUNTS-UE'), contains('ANDALUCISTAS'),
contains('IE'), contains('VOX'), contains('ESCAĂ‘OS EN BLANCO'), contains('CRT'), contains('PODEMOS'), contains('PFAC'), contains('PCTE'), contains('PCTC'),
contains('PCPE/PCPC'), contains('SALAMANCA-ZAMORA-LEĂ“N PREPAL'), contains('P-LIB'), contains('SUMAR'),contains('PACMA'), contains('CREE EN EUROPA'),
contains('PMR'), contains('S.A.L.F'), contains('F'), contains('EXISTE'), contains('Cs'), contains('FE de las JONS'), contains('SAE'), contains('J.U.E.X.'),
contains('PH'), contains('VOLT'), contains('RECORTES CERO'), contains('PAZ SOBERANĂŤA REPĂšBLICA'), contains('FIEL'),contains('IZQP - UNIDOS - DEf'),
contains('EXTREMEĂ‘OS, Y SUBSIDIARIAMENTE EX PREX CREX'), contains('glg'), contains('PIRATES.CAT / REBELDES'), contains('NEX')) |>
mutate_all(~replace_na(., 0))
I got this error:
Error in `pivot_longer()`:
! Can't combine `col2` <double> and `colA1` <character>.
I want to achieve this:
I'm completely stuck. Sorry for long post.