'Row_wise and column_wise fill NA's using fill and down/across
I have the following data and I am trying to "clean" it. I want to do 3 things:
-
- Fill the
NA's in column...2to have something like:
- Fill the
Andalucía Almería
Andalucía Cadiz
Andalucía Cordoba
Andalucía Granada
Andalucía Huelva
...
Aragón Huesca
Aragón Teruel
Aragón Zaragoza
Asturias Asturias
...
So, group_by and
filldownwards.-
- Do the exact same but
row_wise()
- Do the exact same but
-
- Once the
NA's are filled paste the two rows together (and also the two columns together) such as:
- Once the
Andalucía_Almería
Andalucía_Cadiz
Andalucía_Cordoba
Andalucía_Granada
etc.
So, the observations are compressed into a single column and a single row.
Data
df <- structure(list(...2 = c(NA, NA, "España", "Andalucía", NA,
NA, NA, NA, NA, NA, NA, "Aragón", NA, NA, "Asturias", "Baleares (Islas)",
"Canarias", NA, "Cantabria", "Castilla y León", NA, NA, NA,
NA, NA, NA, NA, NA, "Castilla La Mancha", NA, NA, NA, NA, "Cataluña",
NA, NA, NA, "Comunidad Valenciana", NA, NA, "Extremadura", NA,
"Galicia", NA, NA, NA, "Madrid (Comunidad de)", "Murcia (Región de)",
"Navarra (Comunidad Foral de)", "País Vasco", NA, NA, "Rioja (La)",
"Ceuta y Melilla", NA), ...3 = c(NA, NA, NA, "Almería", "Cádiz",
"Córdoba", "Granada", "Huelva", "Jaén", "Málaga", "Sevilla",
"Huesca", "Teruel", "Zaragoza", "Asturias", "Baleares", "Palmas_(Las)",
"Tenerife", "Cantabria", "Avila", "Burgos", "León", "Palencia",
"Salamanca", "Segovia", "Soria", "Valladolid", "Zamora", "Albacete",
"Ciudad_Real", "Cuenca", "Guadalajara", "Toledo", "Barcelona",
"Girona", "Lleida", "Tarragona", "Alicante", "Castellón", "Valencia",
"Badajoz", "Cáceres", "Coruña", "Lugo", "Ourense", "Pontevedra",
"Madrid", "Murcia", "Navarra", "Alava", "Guipúzcoa", "Vizcaya",
"Rioja_(La)", "Ceuta", "Melilla"), ...4 = c("España", NA, "222295",
"5200", "6331", "3285", "4750", "3039", "2969", "9914", "9450",
"1622", "658", "4127", "5046", "5262", "5248", "4614", "3360",
"1178", "2434", "2540", "808", "1710", "721", "502", "2470",
"789", "1818", "2688", "845", "1581", "4014", "21587", "4643",
"2455", "5364", "13776", "4539", "13802", "2607", "1821", "4383",
"1172", "977", "2759", "22573", "8500", "2420", "1486", "2004",
"3969", "1840", "249", "396"), ...5 = c("Andalucía", "Almería",
"4432", "4303", "2", "2", "49", "0", "5", "9", "4", "0", "0",
"0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1",
"0", "0", "3", "0", "0", "0", "0", "0", "1", "0", "1", "7", "1",
"2", "1", "1", "1", "0", "0", "0", "10", "27", "0", "0", "0",
"0", "0", "0", "1"), ...6 = c(NA, "Cádiz", "5622", "4", "5357",
"4", "14", "6", "3", "86", "82", "1", "0", "0", "2", "4", "2",
"1", "2", "0", "3", "1", "0", "3", "0", "0", "1", "0", "0", "4",
"0", "0", "0", "3", "0", "0", "1", "2", "1", "2", "1", "0", "2",
"0", "3", "1", "13", "9", "0", "1", "0", "0", "1", "2", "0"),
...7 = c(NA, "Córdoba", "3463", "20", "25", "3019", "24",
"20", "17", "270", "18", "0", "0", "0", "1", "2", "2", "1",
"0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "1", "5",
"0", "1", "5", "1", "0", "0", "1", "4", "0", "1", "8", "0",
"1", "0", "0", "0", "14", "1", "0", "0", "0", "0", "0", "0",
"0"), ...8 = c(NA, "Granada", "4394", "118", "6", "6", "4067",
"3", "28", "100", "8", "0", "0", "1", "0", "2", "1", "0",
"0", "0", "1", "1", "1", "1", "0", "0", "0", "0", "0", "7",
"0", "0", "2", "3", "5", "0", "1", "5", "4", "0", "0", "0",
"2", "0", "0", "0", "7", "14", "0", "0", "0", "0", "0", "0",
"0"), ...9 = c(NA, "Huelva", "2609", "0", "11", "0", "1",
"2502", "1", "6", "56", "0", "0", "0", "0", "0", "1", "0",
"0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "2",
"0", "0", "0", "0", "0", "0", "0", "1", "0", "1", "15", "0",
"0", "1", "0", "1", "3", "1", "2", "0", "0", "1", "0", "0",
"1"), ...10 = c(NA, "Jaén", "3028", "123", "11", "18", "74",
"0", "2642", "83", "14", "0", "0", "0", "0", "1", "1", "0",
"1", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "6",
"0", "0", "1", "1", "3", "0", "0", "11", "6", "5", "0", "0",
"2", "0", "0", "0", "16", "6", "1", "0", "0", "0", "0", "1",
"0"), ...11 = c(NA, "Málaga", "8862", "24", "77", "36",
"100", "7", "21", "8405", "42", "0", "0", "0", "6", "4",
"4", "8", "3", "2", "3", "1", "0", "2", "0", "0", "0", "0",
"3", "3", "0", "2", "4", "3", "1", "1", "0", "35", "2", "3",
"8", "2", "3", "4", "0", "0", "25", "11", "0", "0", "0",
"1", "0", "1", "5"), ...12 = c(NA, "Sevilla", "10210", "25",
"447", "34", "32", "316", "14", "197", "8975", "0", "0",
"2", "3", "0", "6", "1", "3", "4", "2", "3", "0", "3", "3",
"1", "2", "3", "0", "9", "0", "2", "1", "2", "0", "1", "1",
"7", "5", "5", "52", "5", "1", "0", "1", "6", "25", "5",
"0", "1", "2", "1", "2", "0", "0"), ...13 = c("Aragón",
"Huesca", "991", "1", "1", "1", "2", "0", "0", "1", "0",
"897", "1", "34", "1", "0", "0", "1", "0", "0", "1", "0",
"0", "0", "0", "1", "0", "0", "0", "1", "0", "0", "0", "6",
"0", "6", "15", "0", "9", "2", "1", "0", "0", "0", "0", "0",
"4", "1", "1", "0", "0", "0", "3", "0", "0"), ...14 = c(NA,
"Teruel", "524", "1", "0", "0", "3", "0", "0", "0", "0",
"5", "417", "27", "0", "0", "0", "1", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "3",
"1", "2", "2", "1", "20", "37", "0", "0", "0", "0", "0",
"0", "3", "0", "0", "0", "0", "0", "0", "0", "0"), ...15 = c(NA,
"Zaragoza", "4652", "4", "4", "0", "3", "2", "4", "12", "1",
"264", "38", "3859", "5", "4", "2", "5", "10", "2", "3",
"0", "1", "5", "2", "30", "0", "1", "3", "1", "0", "2", "0",
"6", "6", "10", "100", "34", "134", "20", "0", "2", "3",
"2", "2", "1", "30", "1", "22", "1", "1", "0", "10", "0",
"0"), ...16 = c("Asturias", NA, "4709", "1", "0", "3", "2",
"0", "0", "5", "2", "1", "0", "1", "4446", "4", "2", "8",
"12", "3", "2", "87", "2", "7", "1", "0", "4", "6", "0",
"1", "1", "1", "3", "0", "2", "2", "0", "15", "0", "1", "1",
"2", "7", "28", "3", "6", "31", "3", "1", "1", "0", "1",
"0", "0", "0"), ...17 = c("Baleares", "Baleares", "5047",
"9", "8", "8", "25", "0", "8", "5", "7", "2", "1", "2", "21",
"4776", "26", "10", "2", "0", "3", "4", "4", "2", "0", "0",
"3", "3", "4", "7", "1", "0", "3", "23", "0", "1", "2", "8",
"3", "7", "4", "1", "11", "4", "2", "5", "13", "13", "0",
"0", "1", "1", "3", "0", "1"), ...18 = c("Canarias", "Palmas (Las)",
"5047", "0", "9", "3", "3", "2", "3", "5", "2", "0", "0",
"2", "3", "3", "4883", "44", "4", "1", "0", "1", "0", "2",
"0", "0", "13", "0", "0", "0", "0", "3", "1", "5", "0", "0",
"2", "1", "0", "3", "2", "2", "9", "0", "1", "5", "21", "3",
"0", "0", "0", "0", "1", "0", "5"), ...19 = c(NA, "Tenerife",
"4585", "4", "0", "3", "3", "1", "1", "4", "1", "0", "0",
"1", "5", "3", "112", "4408", "2", "1", "0", "2", "1", "0",
"0", "1", "0", "0", "0", "0", "0", "0", "1", "2", "0", "1",
"0", "6", "2", "0", "1", "1", "0", "0", "2", "3", "9", "1",
"0", "0", "1", "1", "0", "0", "1"), ...20 = c("Cantabria",
"Cantabria", "2742", "4", "2", "0", "3", "0", "1", "5", "5",
"1", "0", "1", "31", "3", "6", "2", "2571", "0", "19", "2",
"15", "1", "0", "0", "2", "1", "0", "3", "0", "1", "2", "2",
"0", "0", "1", "5", "3", "4", "1", "1", "2", "0", "0", "0",
"18", "3", "1", "0", "0", "18", "2", "0", "0"), ...21 = c("Castilla y León",
"Avila", "748", "0", "1", "0", "0", "0", "0", "3", "0", "0",
"0", "0", "5", "1", "0", "0", "2", "665", "2", "0", "0",
"13", "1", "0", "1", "0", "0", "0", "0", "0", "3", "0", "0",
"0", "0", "10", "0", "1", "2", "4", "2", "0", "0", "0", "31",
"1", "0", "0", "0", "0", "0", "0", "0"), ...22 = c(NA, "Burgos",
"2237", "2", "7", "0", "0", "0", "1", "6", "1", "4", "0",
"1", "20", "18", "3", "1", "44", "3", "2020", "2", "11",
"2", "1", "5", "6", "0", "0", "1", "0", "0", "1", "0", "0",
"0", "1", "18", "9", "6", "0", "0", "2", "2", "1", "0", "15",
"3", "2", "9", "1", "4", "4", "0", "0"), ...23 = c(NA, "León",
"2389", "1", "3", "0", "2", "0", "0", "8", "1", "0", "0",
"1", "29", "1", "4", "3", "2", "1", "1", "2211", "1", "8",
"1", "0", "4", "6", "0", "1", "0", "0", "1", "3", "0", "0",
"1", "38", "1", "2", "1", "0", "9", "9", "3", "9", "20",
"2", "0", "0", "0", "1", "0", "0", "0"), ...24 = c(NA, "Palencia",
"745", "0", "0", "0", "0", "1", "0", "4", "0", "0", "0",
"0", "5", "0", "1", "0", "33", "0", "3", "0", "661", "1",
"0", "0", "8", "0", "0", "1", "0", "3", "0", "0", "1", "0",
"0", "3", "1", "4", "0", "1", "4", "0", "0", "0", "8", "1",
"0", "0", "0", "1", "0", "0", "0"), ...25 = c(NA, "Salamanca",
"1530", "1", "6", "0", "1", "1", "0", "7", "2", "0", "0",
"1", "8", "0", "6", "3", "6", "9", "3", "1", "1", "1415",
"1", "2", "9", "4", "0", "0", "0", "0", "3", "0", "0", "0",
"0", "5", "1", "0", "1", "4", "2", "1", "0", "2", "15", "7",
"0", "0", "0", "0", "1", "1", "0"), ...26 = c(NA, "Segovia",
"526", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0",
"0", "1", "0", "1", "0", "2", "4", "5", "0", "0", "0", "438",
"4", "17", "1", "0", "2", "0", "0", "1", "1", "0", "0", "0",
"8", "4", "2", "0", "2", "2", "0", "0", "0", "25", "2", "0",
"0", "0", "1", "0", "0", "0"), ...27 = c(NA, "Soria", "381",
"0", "0", "1", "0", "0", "0", "0", "0", "0", "2", "8", "0",
"0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "346",
"0", "0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "3",
"2", "3", "1", "0", "1", "0", "0", "0", "6", "0", "0", "0",
"0", "0", "3", "0", "0"), ...28 = c(NA, "Valladolid", "2549",
"2", "3", "0", "2", "4", "0", "5", "2", "3", "0", "0", "11",
"0", "2", "8", "40", "7", "15", "19", "27", "5", "5", "0",
"2254", "22", "0", "3", "0", "0", "2", "0", "0", "1", "1",
"29", "4", "2", "0", "1", "25", "3", "2", "2", "27", "6",
"2", "1", "1", "0", "1", "0", "0"), ...29 = c(NA, "Zamora",
"714", "2", "0", "0", "0", "0", "0", "2", "1", "0", "0",
"0", "6", "0", "0", "0", "2", "0", "0", "4", "2", "17", "0",
"0", "12", "636", "0", "1", "0", "0", "0", "0", "0", "0",
"0", "7", "0", "1", "1", "2", "1", "2", "0", "6", "9", "0",
"0", "0", "0", "0", "0", "0", "0"), ...30 = c("Castilla La Mancha",
"Albacete", "1763", "3", "1", "0", "6", "0", "1", "0", "1",
"0", "0", "1", "0", "1", "1", "1", "0", "0", "0", "0", "0",
"1", "0", "1", "1", "0", "1602", "3", "10", "1", "3", "0",
"0", "1", "0", "70", "9", "6", "1", "0", "0", "0", "0", "0",
"9", "28", "1", "0", "0", "0", "0", "0", "0"), ...31 = c(NA,
"Ciudad_Real", "2357", "6", "3", "4", "7", "1", "2", "17",
"0", "0", "0", "0", "2", "0", "0", "0", "0", "1", "1", "3",
"1", "1", "1", "0", "1", "0", "4", "2191", "3", "1", "12",
"0", "0", "0", "0", "33", "1", "2", "5", "1", "0", "2", "2",
"0", "40", "7", "0", "0", "0", "0", "1", "0", "1"), ...32 = c(NA,
"Cuenca", "737", "0", "0", "0", "0", "1", "0", "0", "1",
"0", "0", "0", "2", "0", "0", "0", "0", "0", "0", "0", "0",
"1", "0", "0", "0", "0", "28", "15", "607", "13", "9", "0",
"0", "0", "0", "13", "9", "11", "0", "0", "1", "0", "0",
"0", "22", "3", "0", "0", "0", "0", "1", "0", "0"), ...33 = c(NA,
"Guadalajara", "1098", "13", "4", "1", "1", "1", "2", "4",
"0", "0", "1", "1", "3", "0", "2", "4", "0", "2", "0", "0",
"0", "2", "2", "2", "0", "0", "1", "3", "1", "936", "1",
"3", "1", "0", "0", "23", "8", "10", "1", "3", "1", "2",
"0", "0", "57", "1", "0", "0", "0", "0", "1", "0", "0"),
...34 = c(NA, "Toledo", "2802", "7", "3", "4", "5", "0",
"2", "11", "1", "0", "0", "2", "11", "3", "1", "0", "2",
"10", "0", "1", "0", "1", "1", "0", "0", "1", "0", "43",
"7", "1", "2412", "1", "0", "0", "1", "80", "16", "14", "12",
"18", "2", "0", "0", "0", "118", "10", "1", "0", "0", "0",
"0", "0", "0"), ...35 = c("Cataluña", "Barcelona", "25887",
"109", "38", "63", "94", "24", "69", "62", "70", "106", "66",
"69", "27", "200", "32", "24", "15", "10", "20", "36", "10",
"12", "8", "22", "9", "13", "18", "48", "17", "14", "9",
"21178", "1125", "374", "1204", "69", "145", "93", "68",
"18", "17", "43", "24", "17", "78", "91", "7", "2", "2",
"6", "12", "0", "0"), ...36 = c(NA, "Girona", "3659", "2",
"2", "4", "2", "1", "6", "7", "0", "5", "2", "5", "1", "5",
"7", "1", "2", "0", "0", "1", "0", "4", "0", "3", "2", "0",
"0", "1", "4", "2", "0", "91", "3435", "9", "15", "5", "3",
"3", "5", "6", "1", "1", "2", "2", "2", "7", "1", "0", "0",
"0", "1", "0", "1"), ...37 = c(NA, "Lleida", "2142", "0",
"2", "1", "1", "1", "2", "2", "0", "24", "1", "3", "0", "2",
"0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "0", "0",
"0", "0", "0", "1", "0", "46", "7", "1951", "69", "2", "3",
"3", "2", "0", "4", "1", "1", "6", "1", "2", "0", "0", "0",
"0", "1", "0", "0"), ...38 = c(NA, "Tarragona", "4154", "3",
"3", "7", "7", "0", "9", "2", "2", "20", "10", "12", "7",
"3", "3", "6", "5", "1", "0", "1", "1", "1", "0", "1", "0",
"0", "1", "4", "2", "0", "6", "58", "34", "37", "3845", "3",
"26", "5", "5", "2", "6", "0", "2", "2", "3", "4", "2", "0",
"0", "0", "3", "0", "0"), ...39 = c("Comunidad Valenciana",
"Alicante", "11850", "22", "2", "6", "34", "0", "15", "10",
"3", "2", "2", "3", "11", "7", "3", "1", "3", "2", "3", "5",
"0", "1", "1", "1", "1", "2", "34", "16", "5", "2", "6",
"3", "0", "1", "2", "11337", "10", "53", "5", "1", "14",
"1", "1", "1", "26", "188", "1", "0", "0", "2", "1", "0",
"0"), ...40 = c(NA, "Castellón", "3462", "2", "0", "1",
"3", "0", "2", "0", "1", "2", "7", "1", "0", "6", "0", "2",
"0", "1", "2", "0", "0", "0", "0", "1", "0", "0", "4", "1",
"2", "0", "1", "3", "3", "6", "16", "2", "3346", "40", "2",
"0", "0", "0", "0", "0", "1", "3", "0", "0", "0", "0", "0",
"0", "1"), ...41 = c(NA, "Valencia", "14330", "21", "8",
"6", "6", "1", "18", "8", "4", "12", "106", "4", "8", "47",
"4", "2", "4", "2", "0", "1", "1", "2", "1", "0", "1", "0",
"59", "16", "42", "3", "7", "7", "1", "10", "3", "396", "356",
"13080", "4", "2", "9", "0", "1", "1", "36", "25", "0", "1",
"1", "1", "1", "1", "0"), ...42 = c("Extremadura", "Badajoz",
"2415", "1", "13", "4", "2", "50", "3", "26", "16", "0",
"0", "1", "2", "0", "3", "0", "0", "0", "0", "0", "0", "4",
"1", "0", "0", "1", "0", "6", "0", "0", "3", "1", "0", "0",
"2", "3", "1", "2", "2213", "35", "3", "0", "0", "0", "16",
"0", "1", "0", "1", "1", "0", "0", "0"), ...43 = c(NA, "Cáceres",
"1669", "0", "4", "2", "1", "13", "2", "11", "4", "0", "0",
"1", "1", "2", "1", "0", "1", "1", "0", "1", "0", "23", "0",
"0", "1", "10", "0", "4", "0", "0", "8", "0", "0", "0", "0",
"3", "2", "2", "36", "1502", "2", "0", "1", "1", "21", "6",
"0", "0", "0", "2", "0", "0", "0"), ...44 = c("Galicia",
"Coruña", "3997", "3", "3", "0", "2", "0", "1", "2", "2",
"1", "0", "0", "14", "5", "13", "8", "1", "2", "0", "6",
"0", "0", "0", "1", "0", "1", "0", "0", "0", "0", "3", "4",
"0", "0", "1", "16", "3", "0", "0", "0", "3764", "39", "13",
"59", "17", "9", "0", "0", "0", "0", "3", "0", "1"), ...45 = c(NA,
"Lugo", "1021", "0", "0", "0", "0", "0", "0", "1", "0", "0",
"0", "0", "3", "0", "0", "0", "1", "0", "0", "1", "1", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"1", "1", "0", "0", "0", "0", "55", "920", "3", "23", "3",
"7", "0", "0", "1", "0", "0", "0", "0"), ...46 = c(NA, "Ourense",
"968", "0", "0", "0", "0", "0", "0", "2", "0", "0", "0",
"0", "0", "0", "1", "0", "0", "0", "0", "6", "0", "0", "0",
"0", "0", "3", "0", "0", "0", "1", "0", "1", "1", "0", "2",
"2", "1", "0", "0", "0", "36", "5", "843", "56", "8", "0",
"0", "0", "0", "0", "0", "0", "0"), ...47 = c(NA, "Pontevedra",
"2612", "3", "0", "0", "2", "1", "1", "2", "2", "0", "0",
"1", "3", "3", "20", "8", "1", "0", "0", "0", "1", "2", "0",
"0", "0", "2", "0", "0", "0", "0", "0", "1", "0", "0", "0",
"10", "2", "1", "0", "0", "70", "3", "30", "2431", "9", "1",
"0", "0", "0", "2", "0", "0", "0"), ...48 = c("Madrid", NA,
"30307", "263", "200", "40", "117", "63", "68", "395", "106",
"59", "3", "50", "266", "134", "61", "32", "178", "435",
"60", "102", "27", "108", "247", "65", "97", "49", "27",
"268", "142", "584", "1484", "98", "13", "16", "39", "1056",
"291", "337", "130", "184", "222", "78", "21", "87", "21606",
"300", "52", "3", "9", "16", "17", "1", "1"), ...49 = c("Murcia",
"Murcia", "8096", "66", "4", "0", "18", "0", "8", "12", "1",
"2", "0", "1", "4", "3", "1", "0", "1", "1", "0", "0", "0",
"2", "0", "0", "1", "0", "25", "3", "0", "2", "4", "5", "0",
"2", "0", "226", "1", "8", "0", "0", "2", "2", "0", "3",
"19", "7668", "1", "0", "0", "0", "0", "0", "0"), ...50 = c("Navarra",
"Navarra", "2531", "3", "6", "1", "1", "4", "3", "19", "0",
"45", "0", "18", "10", "5", "0", "5", "10", "0", "5", "1",
"2", "4", "0", "2", "1", "0", "0", "4", "1", "0", "3", "6",
"0", "2", "17", "25", "26", "2", "3", "1", "7", "1", "1",
"1", "14", "5", "2229", "1", "7", "3", "27", "0", "0"), ...51 = c("País Vasco",
"Alava", "1721", "1", "6", "0", "2", "0", "0", "7", "0",
"4", "1", "1", "4", "0", "6", "3", "28", "1", "33", "10",
"3", "6", "0", "0", "2", "5", "0", "0", "0", "0", "1", "2",
"0", "1", "3", "24", "11", "2", "1", "5", "7", "2", "2",
"3", "9", "1", "9", "1426", "12", "14", "63", "0", "0"),
...52 = c(NA, "Guipúzcoa", "2570", "2", "10", "0", "4",
"2", "2", "7", "3", "127", "0", "3", "8", "2", "11", "2",
"20", "2", "12", "11", "14", "25", "2", "7", "7", "3", "0",
"2", "0", "1", "0", "2", "0", "10", "6", "36", "20", "3",
"7", "2", "35", "5", "4", "6", "14", "3", "62", "5", "1942",
"17", "102", "0", "0"), ...53 = c(NA, "Vizcaya", "5097",
"14", "17", "1", "14", "8", "4", "41", "4", "31", "0", "4",
"45", "5", "11", "7", "328", "2", "204", "13", "18", "22",
"2", "4", "6", "16", "1", "0", "0", "4", "4", "10", "3",
"9", "3", "60", "27", "9", "4", "9", "28", "11", "6", "13",
"32", "9", "9", "14", "21", "3873", "117", "0", "0"), ...54 = c("Rioja (La)",
"Rioja (La)", "1591", "1", "4", "0", "1", "1", "0", "1",
"1", "4", "0", "5", "3", "3", "1", "1", "19", "0", "7", "1",
"0", "0", "0", "0", "3", "0", "0", "0", "0", "0", "0", "2",
"0", "0", "5", "18", "5", "1", "0", "1", "3", "0", "0", "0",
"8", "0", "12", "20", "1", "1", "1458", "0", "0"), ...55 = c("Ceuta y Melilla",
"Ceuta", "292", "0", "17", "2", "5", "2", "0", "11", "3",
"0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0",
"0", "0", "0", "2", "0", "2", "0", "0", "0", "0", "0", "3",
"0", "0", "0", "0", "0", "0", "242", "0"), ...56 = c(NA,
"Melilla", "431", "5", "0", "0", "5", "0", "0", "26", "0",
"0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0",
"0", "1", "0", "1", "0", "0", "1", "0", "0", "3", "0", "0",
"0", "0", "0", "3", "1", "0", "0", "2", "0", "0", "0", "3",
"1", "0", "0", "0", "0", "0", "0", "377"), ...57 = c(NA,
NA, 183, 0, 0, 0, 11, 0, 0, 0, 3, 0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 2, 27, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 134)), row.names = c(NA,
-55L), class = c("tbl_df", "tbl", "data.frame"))
Edit:
Expected output:
Would be to have a data frame/matrix where the rownames are the items on the left and the column names are the items across the top.
The data looks like:
# A tibble: 55 × 56
...2 ...3 ...4 ...5 ...6 ...7 ...8 ...9 ...10 ...11 ...12 ...13 ...14 ...15 ...16 ...17 ...18 ...19 ...20 ...21 ...22 ...23 ...24 ...25 ...26 ...27 ...28 ...29 ...30 ...31
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 NA NA Espa… Anda… NA NA NA NA NA NA NA Arag… NA NA Astu… Bale… Cana… NA Cant… Cast… NA NA NA NA NA NA NA NA Cast… NA
2 NA NA NA Alme… Cádiz Córd… Gran… Huel… Jaén Mála… Sevi… Hues… Teru… Zara… NA Bale… Palm… Tene… Cant… Avila Burg… León Pale… Sala… Sego… Soria Vall… Zamo… Alba… Ciud…
3 España NA 2222… 4432 5622 3463 4394 2609 3028 8862 10210 991 524 4652 4709 5047 5047 4585 2742 748 2237 2389 745 1530 526 381 2549 714 1763 2357
4 Andal… Alme… 5200 4303 4 20 118 0 123 24 25 1 1 4 1 9 0 4 4 0 2 1 0 1 1 0 2 2 3 6
5 NA Cádiz 6331 2 5357 25 6 11 11 77 447 1 0 4 0 8 9 0 2 1 7 3 0 6 1 0 3 0 1 3
6 NA Córd… 3285 2 4 3019 6 0 18 36 34 1 0 0 3 8 3 3 0 0 0 0 0 0 0 1 0 0 0 4
7 NA Gran… 4750 49 14 24 4067 1 74 100 32 2 3 3 2 25 3 3 3 0 0 2 0 1 0 0 2 0 6 7
8 NA Huel… 3039 0 6 20 3 2502 0 7 316 0 0 2 0 0 2 1 0 0 0 0 1 1 0 0 4 0 0 1
9 NA Jaén 2969 5 3 17 28 1 2642 21 14 0 0 4 0 8 3 1 1 0 1 0 0 0 0 0 0 0 1 2
10 NA Mála… 9914 9 86 270 100 6 83 8405 197 1 0 12 5 5 5 4 5 3 6 8 4 7 0 0 5 2 0 17
Solution 1:[1]
This was what I was hoping for:
out <- df %>%
fill(`...2`, .direction = c("down")) %>%
unite(col = "CCAA", c(1, 2)) %>%
mutate(
"CCAA" = gsub("NA_NA|_NA|NA", "", CCAA)
) %>%
t() %>%
as.data.frame() %>%
fill(V1, .direction = "down") %>%
unite(col = "CCAA", c(1, 2)) %>%
mutate(
"CCAA" = gsub("NA_NA|_NA|NA", "", CCAA)
) %>%
t() %>%
as.data.frame() %>%
janitor::row_to_names(1) %>%
rename(CCAA = c(1)) %>%
remove_rownames() %>%
column_to_rownames("CCAA") %>%
tibble()
Which gives:
# A tibble: 53 × 54
España Andalucía_Almería Andalucía_Cádiz Andalucía_Córdoba Andalucía_Granada Andalucía_Huelva Andalucía_Jaén Andalucía_Málaga Andalucía_Sevilla Aragón_Huesca Aragón_Teruel
<fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 222295 4432 5622 3463 4394 2609 3028 8862 10210 991 524
2 5200 4303 4 20 118 0 123 24 25 1 1
3 6331 2 5357 25 6 11 11 77 447 1 0
4 3285 2 4 3019 6 0 18 36 34 1 0
5 4750 49 14 24 4067 1 74 100 32 2 3
6 3039 0 6 20 3 2502 0 7 316 0 0
7 2969 5 3 17 28 1 2642 21 14 0 0
8 9914 9 86 270 100 6 83 8405 197 1 0
9 9450 4 82 18 8 56 14 42 8975 0 0
10 1622 0 1 0 0 0 0 0 0 897 5
Solution 2:[2]
package:firebase_auth renamed its FirebaseUser class to just User. Presumably you tried to fix this by doing a search and replace through your code to change FirebaseUser to just User, but this created a name collision with your own User class. You then tried to avoid this collision by adding a library prefix for your class but didn't properly fix all use sites.
(The Firebase User class does not provide any public constructors, which caught the error.)
You have a function:
//create a user object based on FirebaseUser (the return type is user then)
//underscore cz this is a private function that we can only use here.
//if it's true return the uid, else return null.
// ignore: unused_element
User? _userFromFirebaseUser(User? user) {
// ignore: unnecessary_null_comparison
if (user !=null) {
return User(uid: user.uid);
}else{
return null;
}
}
which currently doesn't make sense because it takes a User as an argument and then returns the same type. The function name and description indicate that you instead intend to accept a Firebase User object and to return your own User object, presumably from package:irrigationapp/models/user.dart which you've prefixed with UserModel. You therefore would need to change it to:
//create a user object based on FirebaseUser (the return type is user then)
//underscore cz this is a private function that we can only use here.
//if it's true return the uid, else return null.
// ignore: unused_element
UserModel.User? _userFromFirebaseUser(User? user) {
// ignore: unnecessary_null_comparison
if (user !=null) {
return UserModel.User(uid: user.uid);
}else{
return null;
}
}
(Personally I think it'd be better to add a library prefix for the Firebase classes instead, though.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user113156 |
| Solution 2 | jamesdlin |
