1

Intento tabular un dataframe,

Tengo varias columnas c("Año","Municipio","Población","Esfuerzo Social","Esfuerzo Total")

El resultado buscado para cada municipio es una columna por año de las variables Población, Esfuerzo Social y Esfuerzo Total, es decir, para cada variable Población, Esfuerzo Social y Esfuerzo Total ha de haber una columna para cada uno de los valores de la variable Año

Este es el dataframe:


structure(list(`Código Municipio INE` = c("02003", "02003", "02003", 
"03014", "03014", "03014", "04013", "04013", "04013", "05019", 
"05019", "06015", "06015", "06015", "07040", "07040", "07040", 
"08019", "08019", "08019", "09059", "09059", "09059", "10037", 
"10037", "10037", "11012", "11012", "11012", "12040", "12040", 
"12040", "13034", "13034", "13034", "14021", "14021", "14021", 
"15030", "15030", "15030", "16078", "16078", "16078", "17079", 
"17079", "17079", "18087", "18087", "18087", "19130", "19130", 
"19130", "21041", "21041", "21041", "22125", "22125", "22125", 
"23050", "23050", "23050", "24089", "24089", "24089", "25120", 
"25120", "25120", "26089", "26089", "26089", "27028", "27028", 
"27028", "28079", "28079", "28079", "29067", "29067", "29067", 
"30030", "30030", "30030", "32054", "32054", "32054", "33044", 
"33044", "33044", "34120", "34120", "34120", "35016", "35016", 
"35016", "36038", "36038", "36038", "37274", "37274", "37274", 
"38038", "38038", "38038", "39075", "39075", "39075", "40194", 
"40194", "40194", "41091", "41091", "41091", "42173", "42173", 
"42173", "43148", "43148", "43148", "44216", "44216", "44216", 
"45168", "45168", "45168", "46250", "46250", "46250", "47186", 
"47186", "47186", "49275", "49275", "49275", "50297", "50297", 
"50297", "51001", "51001", "51001", "52001", "52001", "52001"
), Año = c("2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017", 
"2015", "2016", "2017", "2015", "2016", "2017", "2015", "2016", 
"2017", "2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017", 
"2015", "2016", "2017", "2015", "2016", "2017", "2015", "2016", 
"2017", "2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017", 
"2015", "2016", "2017", "2015", "2016", "2017", "2015", "2016", 
"2017", "2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017", 
"2015", "2016", "2017", "2015", "2016", "2017", "2015", "2016", 
"2017", "2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017", 
"2015", "2016", "2017", "2015", "2016", "2017", "2015", "2016", 
"2017", "2015", "2016", "2017", "2015", "2016", "2017", "2015", 
"2016", "2017", "2015", "2016", "2017", "2015", "2016", "2017"
), Municipio = c("Albacete", "Albacete", "Albacete", "Alicante/Alacant", 
"Alicante/Alacant", "Alicante/Alacant", "Almería", "Almería", 
"Almería", "Ávila", "Ávila", "Badajoz", "Badajoz", "Badajoz", 
"Palma", "Palma", "Palma", "Barcelona", "Barcelona", "Barcelona", 
"Burgos", "Burgos", "Burgos", "Cáceres", "Cáceres", "Cáceres", 
"Cádiz", "Cádiz", "Cádiz", "Castellón de la Plana/Castelló de la Plana", 
"Castellón de la Plana/Castelló de la Plana", "Castellón de la Plana/Castelló de la Plana", 
"Ciudad Real", "Ciudad Real", "Ciudad Real", "Córdoba", "Córdoba", 
"Córdoba", "Coruña (A)", "Coruña (A)", "Coruña (A)", "Cuenca", 
"Cuenca", "Cuenca", "Girona", "Girona", "Girona", "Granada", 
"Granada", "Granada", "Guadalajara", "Guadalajara", "Guadalajara", 
"Huelva", "Huelva", "Huelva", "Huesca", "Huesca", "Huesca", "Jaén", 
"Jaén", "Jaén", "León", "León", "León", "Lleida", "Lleida", "Lleida", 
"Logroño", "Logroño", "Logroño", "Lugo", "Lugo", "Lugo", "Madrid", 
"Madrid", "Madrid", "Málaga", "Málaga", "Málaga", "Murcia", "Murcia", 
"Murcia", "Ourense", "Ourense", "Ourense", "Oviedo", "Oviedo", 
"Oviedo", "Palencia", "Palencia", "Palencia", "Palmas de Gran Canaria (Las)", 
"Palmas de Gran Canaria (Las)", "Palmas de Gran Canaria (Las)", 
"Pontevedra", "Pontevedra", "Pontevedra", "Salamanca", "Salamanca", 
"Salamanca", "Santa Cruz de Tenerife", "Santa Cruz de Tenerife", 
"Santa Cruz de Tenerife", "Santander", "Santander", "Santander", 
"Segovia", "Segovia", "Segovia", "Sevilla", "Sevilla", "Sevilla", 
"Soria", "Soria", "Soria", "Tarragona", "Tarragona", "Tarragona", 
"Teruel", "Teruel", "Teruel", "Toledo", "Toledo", "Toledo", "Valencia", 
"Valencia", "Valencia", "Valladolid", "Valladolid", "Valladolid", 
"Zamora", "Zamora", "Zamora", "Zaragoza", "Zaragoza", "Zaragoza", 
"Ceuta", "Ceuta", "Ceuta", "Melilla", "Melilla", "Melilla"), 
    Población = c(172121L, 172426L, 172816L, 328648L, 330525L, 
    329988L, 194203L, 194515L, 195389L, 58083L, 58149L, 149892L, 
    149946L, 150543L, 400578L, 402949L, 406492L, 1604555L, 1608746L, 
    1620809L, 177100L, 176608L, 175623L, 95617L, 95814L, 95917L, 
    120468L, 118919L, 118048L, 171669L, 170990L, 169498L, 74427L, 
    74054L, 74641L, 327362L, 326609L, 325916L, 243870L, 243978L, 
    244099L, 55428L, 55102L, 54876L, 97586L, 98255L, 99013L, 
    235800L, 234758L, 232770L, 83391L, 83633L, 84145L, 146318L, 
    145468L, 145115L, 52239L, 52282L, 52223L, 115395L, 114658L, 
    114238L, 127817L, 126192L, 125317L, 138542L, 138144L, 137327L, 
    151344L, 150876L, 150979L, 98134L, 98268L, 97995L, 3141991L, 
    3165541L, 3182981L, 569130L, 569009L, 569002L, 439889L, 441003L, 
    443243L, 106231L, 105893L, 105636L, 221870L, 220567L, 220301L, 
    79595L, 79137L, 78892L, 379766L, 378998L, 377650L, 82539L, 
    82549L, 82671L, 146438L, 144949L, 144436L, 203811L, 203585L, 
    203692L, 173957L, 172656L, 171951L, 52728L, 52257L, 51756L, 
    693878L, 690566L, 689434L, 39168L, 39171L, 38881L, 131255L, 
    131094L, 131507L, 35590L, 35564L, 35484L, 83226L, 83459L, 
    83741L, 786189L, 790201L, 787808L, 303905L, 301876L, 299715L, 
    63831L, 63217L, 62389L, 664953L, 661108L, 664938L, 84263L, 
    84519L, 84959L, 85584L, 86026L, 86120L), `Esfuerzo Social` = c(12353570.26, 
    10922141.66, 11450013.65, 15963858.06, 16781422.67, 18298031.66, 
    14867434.39, 14500734.62, 13157434.34, 9458354.23, 9271539.84, 
    5621106.01, 5544874.22, 6009806.4, 28499840.09, 28263531.47, 
    30747350.88, 269377354.25, 269687878.57, 311009897.1, 9847253.6, 
    11682897.44, 11662432.69, 3516333, 3854313.85, 3987239.66, 
    15119782.93, 15223387.62, 15573839.74, 10220700.43, 10884960.63, 
    12003103.14, 4305129.22, 4493823.61, 4729076.99, 32917425.6, 
    28064436.03, 32559749.92, 21302157.38, 20479378.22, 22499515.58, 
    2361489.1, 2696353.37, 2117289.71, 9988574.72, 10042995.31, 
    9088724.66, 23212458.85, 23928882.64, 25312197.83, 3515746.67, 
    3704068.37, 3603573.47, 11779688.78, 12099911.63, 9753172.03, 
    2535171.2, 2794195.55, 2745971.76, 11087890.83, 12657489.6, 
    11741169.23, 9633918.63, 10653275.15, 10250409.9, 21627620.03, 
    11339588.65, 11976310.93, 11678201.27, 11588866.11, 12508845.41, 
    6354079.57, 7251694.22, 6295505.75, 319002813.85, 336765246.88, 
    370002842.09, 48087330.09, 50183361.26, 54310973, 23936966.64, 
    25208156.09, 25674095.7, 6591149.97, 5163582.52, 7913419.45, 
    14263042.19, 16110379.54, 16524331.3, 5235375.12, 5385022.46, 
    5707501.06, 45499446.01, 43516030.57, 49626117.33, 2965094.16, 
    2918548.92, 3097844.98, 9098495.22, 9690902.6, 10244355.63, 
    18086427.14, 20809565.23, 24383554.51, 7703290.38, 7333144.4, 
    9699415.62, 3539691, 3851895.54, 3960011.56, 59275193.9, 
    61950362.27, 70918490.64, 6175033.34, 6321130.67, 6062631.85, 
    11165492.12, 11904640.63, 12632395.97, 1763313.67, 1873380.28, 
    2023896.31, 3468411.84, 3589493.14, 3626506.31, 56736581.32, 
    59832670.73, 65963215.33, 21597101.58, 19558134.83, 20634437.72, 
    2780114.43, 3145283.83, 3617142.22, 69832389.44, 74653628.26, 
    75028650.45, 14000344.92, 15119434.03, 16073267.92, 32076143.64, 
    35012561.82, 37133673.83), `Esfuerzo Total` = c(146336577.74, 
    144001222.43, 146900702.41, 274437886.16, 255653667.86, 242141152.77, 
    170271509.03, 158297508.18, 158339559.37, 51025307.05, 51162661.04, 
    104635468.81, 129356260.33, 93091788.49, 403776960.41, 406988326.44, 
    442573789.35, 2863263170.77, 2776640464.76, 2767429667.27, 
    181895321.7, 177179557.98, 197229750.4, 67550236.14, 65487414.2, 
    72604676.71, 154741114.03, 147976479.38, 145095608.88, 148452089.03, 
    152564946.94, 178543220.9, 64040880.16, 64745398.16, 76024274.27, 
    291746009.37, 269000136.8, 305747034.65, 238591283.22, 217199329.97, 
    227447212.74, 55210772.61, 53350392.52, 48751853.68, 118976936.23, 
    117997137.22, 113432066.54, 260474161.15, 258739020.49, 274200741.57, 
    78113679.24, 63774425.87, 64507652.1, 106752012.11, 109322964.15, 
    119384591.53, 54951779.85, 45192599.75, 48202704.12, 156066667.24, 
    118250506.52, 130151785.6, 124751717.72, 139513886.58, 134829503.35, 
    153572785.3, 149440154.19, 152315670.12, 135149127.56, 130898825.25, 
    135870513.69, 84335771.96, 88028341.41, 76076444.14, 4409536506.99, 
    4788757430.06, 4257036746.64, 583751792.77, 614977804.71, 
    608574263.8, 381446450.71, 380020463.64, 363999558.04, 107553442.94, 
    96407948.9, 92771997.68, 205957732.52, 195377347.73, 238110024.4, 
    68218816.99, 63993765.55, 67694380.74, 305535009.3, 303136532.22, 
    327379174.56, 68298349.95, 68121278.49, 66693094.61, 150082935.78, 
    140176815.87, 142200689.59, 200250522.69, 204588380.34, 203382556.79, 
    174179813.47, 172366586.82, 192494726.65, 57978774.97, 64477788.36, 
    63425075.01, 739698610.72, 738936525.09, 734690074.96, 47428843.58, 
    37703999.81, 38006349.91, 161231015.95, 180820707.05, 170924880.92, 
    29352061.33, 26507583.53, 29132086.12, 105104293.36, 89009650.48, 
    88363542.15, 1001956983.6, 747514692.26, 770687365.33, 265003928.39, 
    246596858, 261926908.17, 50666451.54, 57176567.76, 52564110.51, 
    754417763.41, 687977382.66, 734538384.99, 275230668.17, 254832077.57, 
    253416368.24, 240640468.48, 239046287.37, 249139842.84)), row.names = c(NA, 
143L), class = "data.frame")

ACTUALIZACIÓN

De tal manera que pueda presentar una tabla donde para cada una de esas variables Población Esfuerzo Social Esfuerzo Total se presente una columna de cada año.

Aunque rmarkdown no lo pueda representar sería algo así:


            |Población           |Esfuerzo Social     |Esfuerzo Total      |
            |--------------------|--------------------|--------------------|
            ||2015| |2016| |2017|||2015| |2016| |2017|||2015| |2016| |2017||
            ||----| |----| |----|||----| |----| |----|||----| |----| |----||
|Municipio| ||    | |    | |    |||    | |    | |    |||    | |    | |    ||

1 respuesta 1

2

si entendí bien tu problema podrías aplicar la siguiente función para pivotear las columnas deseadas:

library(tidyverse)

df1 = pivot_wider(df,
  names_from = c("Año"),
  values_from = c("Población", "Esfuerzo Social", "Esfuerzo Total")
  )

Es decir, tu df se pivoteara respecto a la variable "Año" y los valores serán obtenidos de las variables "Población", "Esfuerzo Social", "Esfuerzo Total" para cada categoria que contenga tu variable "Año"

3
  • Hola, gracias por responder, he actualizado la pregunta, necesito que para cada una de las variables Población Esfuerzo Social y Esfuerzo total las pueda mostrar en una tabla donde para cada una de ellas se presente una columna por año. Commented el 11 may. 2021 a las 20:48
  • Sí, como has respondido, sería una opción el código de tu respuesta, indicando también, id_cols: df <- pivot_wider ( data = df, id_cols = c("Municipio"), names_from = c("Año"), values_from = c("Población", "Esfuerzo Social", "Esfuerzo Total") ) Commented el 11 may. 2021 a las 21:41
  • Para el ejemplo que he propuesto, el resultado es el mismo sin definir id_cols. Es una opción válida como solución. Commented el 12 may. 2021 a las 1:39

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.