Home > Software design >  Create n period differences in a panel in R
Create n period differences in a panel in R

Time:05-25

I'm trying to figure out a way to calculate differences between many variables in a panel.

I've found this piece of code, from this post How do I Difference Panel Data in R

MyData %>% 
  group_by(country) %>% 
  mutate(x1d = x - lag(x)) %>% 
  ungroup

and it works for one variable, but I have a panel with over 50 variables, so I would like to find a way to automate the third line of the code (the mutate part), so that I don't have to write it down for each of the variables.

my data looks like this

structure(list(country = c("Albania", "Albania", "Albania", "Albania", 
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania", 
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania", 
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania", 
"Albania", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", 
"Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria", 
"Algeria", "Algeria"), year = c(1998, 1999, 2000, 2001, 2002, 
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015, 2016, 2017, 2018, 2019, 2020, 1960, 1961, 1962, 1963, 
1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972), grants_total = c(230.25, 
511.4, 344.63, 317.42, 278.73, 325.07, 238.83, 272.91, 276.49, 
232.62, 278.1, 253.41, 254.93, 238.71, 232.48, 188.71, 225.7, 
288.25, 226.51, 226.99, 276.48, 259.22, 360.09, 3032.13, 3521.31, 
2895.89, 1967.33, 1525.3, 1007.18, 880.77, 769.12, 803.05, 732.66, 
649.14, 505.88, 448.26), oda_grants_ad = c(132.95, 173.41, 214.3, 
189.6, 123.8, 124.98, 117.84, 165.61, 160.26, 150.34, 183.98, 
169.35, 159.29, 152.11, 141.39, 131.64, 152.45, 112.39, 138.95, 
132.04, 177.33, 144.18, 133.04, 3032.53, 3521.23, 2885.89, 1920.9, 
1500.17, 991.16, 548.1, 447.3, 513.03, 414.65, 359.94, 258.67, 
196.52), total_oda_gross = c(380.12, 707.17, 507.41, 443.97, 
476.86, 465.13, 379.82, 385.9, 392.8, 343.8, 377.17, 382.8, 408.3, 
400.95, 386.64, 320.37, 347.23, 431.37, 325.79, 297.88, 466.61, 
322.14, 442.43, 3032.13, 3985.6, 3314.24, 2214.63, 1728.65, 1107.76, 
963.39, 792.6, 854.69, 921.23, 861, 814.87, 659.74), total_oda_gross_ad = c(282.82, 
369.18, 377.08, 316.15, 321.93, 265.04, 258.83, 278.6, 276.57, 
261.52, 283.05, 298.74, 312.66, 314.35, 295.55, 263.3, 273.98, 
255.51, 238.23, 202.93, 367.46, 207.1, 215.38, 3032.53, 3985.52, 
3304.24, 2168.2, 1703.52, 1091.74, 630.72, 470.78, 564.67, 603.22, 
571.8, 567.66, 408), oda_total_net = c(379.07, 701.24, 494.46, 
426.42, 455.57, 440.58, 351.89, 355.82, 357.12, 307.16, 336.77, 
338.09, 353.24, 345.48, 335.08, 254.03, 262.73, 333.8, 180.79, 
176.21, 344.01, 28.53, 309.94, 3217.1, 3846.67, 3254.95, 2192.23, 
1691.99, 1074.48, 891, 751.59, 805.59, 863.45, 804.68, 769.42, 
595.41), total_oda_net_ad = c(281.77, 363.25, 364.13, 298.6, 
300.64, 240.49, 230.9, 248.52, 240.89, 224.88, 242.65, 254.03, 
257.6, 258.88, 243.99, 196.96, 189.48, 157.94, 93.23, 81.26, 
244.86, -86.51, 82.89, 3217.5, 3846.59, 3244.95, 2145.8, 1666.86, 
1058.46, 558.33, 429.77, 515.57, 545.44, 515.48, 522.21, 343.67
), region = c("Europe", "Europe", "Europe", "Europe", "Europe", 
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe", 
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe", 
"Europe", "Europe", "Europe", "Europe", "Africa", "Africa", "Africa", 
"Africa", "Africa", "Africa", "Africa", "Africa", "Africa", "Africa", 
"Africa", "Africa", "Africa"), v2x_polyarchy = c(0.401, 0.404, 
0.414, 0.443, 0.492, 0.499, 0.5, 0.522, 0.548, 0.551, 0.552, 
0.551, 0.546, 0.546, 0.543, 0.51, 0.518, 0.521, 0.521, 0.538, 
0.52, 0.494, 0.501, 0.077, 0.077, 0.093, 0.149, 0.187, 0.132, 
0.084, 0.084, 0.084, 0.084, 0.084, 0.084, 0.084), v2x_libdem = c(0.331, 
0.335, 0.347, 0.37, 0.401, 0.403, 0.406, 0.42, 0.437, 0.44, 0.442, 
0.446, 0.444, 0.444, 0.441, 0.421, 0.431, 0.433, 0.433, 0.43, 
0.421, 0.407, 0.403, 0.038, 0.038, 0.044, 0.08, 0.086, 0.076, 
0.065, 0.065, 0.065, 0.065, 0.065, 0.065, 0.065), v2x_partipdem = c(0.261, 
0.271, 0.277, 0.292, 0.319, 0.323, 0.333, 0.348, 0.361, 0.362, 
0.361, 0.36, 0.356, 0.355, 0.351, 0.318, 0.323, 0.322, 0.321, 
0.333, 0.322, 0.312, 0.316, 0.053, 0.034, 0.035, 0.053, 0.051, 
0.042, 0.034, 0.034, 0.033, 0.034, 0.033, 0.032, 0.032), v2x_delibdem = c(0.317, 
0.316, 0.325, 0.363, 0.388, 0.39, 0.397, 0.435, 0.444, 0.425, 
0.428, 0.42, 0.41, 0.419, 0.415, 0.246, 0.265, 0.278, 0.271, 
0.246, 0.231, 0.222, 0.221, 0.087, 0.075, 0.149, 0.129, 0.138, 
0.125, 0.114, 0.114, 0.114, 0.114, 0.102, 0.102, 0.102), v2x_egaldem = c(0.293, 
0.295, 0.308, 0.327, 0.361, 0.365, 0.365, 0.375, 0.388, 0.391, 
0.39, 0.389, 0.388, 0.388, 0.385, 0.369, 0.38, 0.375, 0.375, 
0.377, 0.376, 0.352, 0.372, 0.051, 0.051, 0.103, 0.203, 0.215, 
0.199, 0.185, 0.185, 0.185, 0.185, 0.183, 0.183, 0.183), corruption = c(0.778, 
0.815, 0.786, 0.786, 0.81, 0.81, 0.81, 0.777, 0.777, 0.777, 0.798, 
0.798, 0.798, 0.798, 0.784, 0.82, 0.838, 0.838, 0.838, 0.852, 
0.842, 0.836, 0.836, 0.268, 0.268, 0.278, 0.298, 0.298, 0.254, 
0.254, 0.254, 0.254, 0.254, 0.254, 0.254, 0.254), dem = c(0.3206, 
0.3242, 0.3342, 0.359, 0.3922, 0.396, 0.4002, 0.42, 0.4356, 0.4338, 
0.4346, 0.4332, 0.4288, 0.4304, 0.427, 0.3728, 0.3834, 0.3858, 
0.3842, 0.3848, 0.374, 0.3574, 0.3626, 0.0612, 0.055, 0.0848, 
0.1228, 0.1354, 0.1148, 0.0964, 0.0964, 0.0962, 0.0964, 0.0934, 
0.0932, 0.0932), total_ETR_L = c(4536886471502868992, 5591675814249663488, 
6859387973195783168, 7749788519167251456, 8546802313760017408, 
9501423857806553088, 11161916934635896832, 13530996363722708992, 
15613046753542662144, 17904040562498834432, 22414046182794457088, 
23744831551876059136, 25551009437347557376, 25521085289383919616, 
2.5806490506081e 19, 25428493843732750336, 29310281093782241280, 
30348094285190770688, 33417251175192182784, 36969906606257184768, 
40339011590312673280, NA, NA, NA, NA, NA, NA, NA, 84475912775976386560, 
70084402116367556618, 73513994394282500096, 7.6940301033468e 19, 
1.03912463899794e 20, 1.20254311817552e 20, 89775615873509605376, 
1.47799434159577e 20), total_ETR_K = c(2968783027328335360, 4616919531014105088, 
6213343377823513600, 7949200562048785408, 9427270416414924800, 
10840707426791243776, 13400453087032227840, 13098693707470735360, 
14572008822711111680, 15542401188019382272, 16752053007524777984, 
19041560691277385728, 17368423285839374336, 19720225157155639296, 
19133809795732361216, 1.7546493982233e 19, 2.1115084198123e 19, 
23601259869049401344, 1.843144180334e 19, 30526933755520233472, 
32183600864262647808, NA, NA, NA, NA, NA, NA, NA, 1.46775625177444e 20, 
1.27429470918953e 20, 1.43434031827787e 20, 1.65361341966337e 20, 
2.09077764872508e 20, 2.70920345609891e 20, 2.43768794966909e 20, 
3.91143207614129e 20), ETR_L = c(299949593.536758, 365259852.029152, 
395388819.127655, 441186665.409904, 477965296.196651, 503603961.506218, 
567778748.218315, 596324298.739555, 637166642.531387, 711942531.966248, 
883126346.580811, 936407903.724274, 951403672.965958, 891453934.106689, 
914362765.927109, 929262425.096306, 1036820633.97732, 1058047511.62762, 
1179368741.44936, 1286883689.506, 1385930287.57062, NA, NA, NA, 
NA, NA, NA, NA, 2730680183.0658, 2381192401.24182, 2148664132.9178, 
1856163805.76816, 2389087255.01587, 2539268958.42368, 2066475155.43677, 
2778787394.33203), ETR_K = c(436516534.915085, 641761570.262901, 
815576838.642311, 990486591.921722, 1143683920.00786, 1252369750.83369, 
1480428035.08208, 1302596484.90593, 1349358162.12268, 1335410881.82373, 
1289983860.14282, 1458517701.39087, 1209555324.72565, 1381861017.29956, 
1312913125.43607, 1172465136.605, 1401383639.97771, 1570101711.38095, 
1051998876.56806, 1943914115.2261, 1956308804.49109, NA, NA, 
NA, NA, NA, NA, NA, 5846952803.41187, 5410257002.76361, 5513222836.36642, 
5676770887.60144, 6615523558.70398, 7968575017.05237, 8307963371.8457, 
10396582801.7344), indirect_taxes = c(722887176.783981, 737186889.177048, 
880244136.20739, 885246860.189621, 925937933.172569, 981391227.225235, 
1089752383.31676, 1330187418.61043, 1473527958.59024, 1572791531.53931, 
1760362441.26587, 1773925477.61337, 1872931780.38182, 1876275859.14651, 
1843092527.00381, 1758666560.20605, 2015323921.93304, 2036249866.70065, 
2106465982.91643, 2206817289.45598, 2265848241.47195, NA, NA, 
NA, NA, NA, NA, NA, 1487643147.15042, 1327349329.68093, 1477781422.80531, 
1664975511.97618, 1955547235.60565, 2070776108.46506, 1784619535.09766, 
2208404474.12109), total_tax = c(1459353305.23582, 1744208311.4691, 
2091209793.97736, 2316920117.52125, 2547587149.37708, 2737364939.56515, 
3137959166.61716, 3229108202.25591, 3460052763.24431, 3620144945.32928, 
3933472647.9895, 4168851082.72852, 4033890778.07343, 4149590810.55276, 
4070368418.36699, 3860394121.90736, 4453528195.88807, 4664399089.70923, 
4337833600.93385, 5437615094.18809, 5608087333.53366, NA, NA, 
NA, NA, NA, NA, NA, 10065276133.6281, 9118798733.68636, 9139668392.08953, 
9197910205.34578, 10960158049.3255, 12578620083.9411, 12159058062.3801, 
15383774670.1875), inv_pub_imf = c(625529766.082764, 1004400849.34235, 
853801012.039185, 995071470.737457, 958180844.783783, 701442718.505859, 
838291823.863983, 1116348743.43872, 1242379426.95618, 1432086706.1615, 
2117090463.63831, 2156784534.45435, 1487938642.50183, 1554694294.9295, 
1339423418.04504, 1458708643.91327, 1490239858.62732, 1442538261.41357, 
1427073121.07086, 1616881370.54443, 1726246953.01056, 1677332401.27563, 
NA, 3109152078.62854, 3233517885.20813, 3362858295.44067, 3497372627.2583, 
3637267589.56909, 3782758235.9314, 3934068441.39099, 4091431140.89966, 
4255088329.31519, 4425291538.23853, 4130040168.76221, 4132284164.42871, 
4897582530.97534), inv_priv_imf = c(1549019098.28186, 1815057873.72589, 
3040284872.05505, 3783464431.7627, 4067460536.95679, 4720480442.04712, 
5223311901.09253, 5373108386.99341, 5738309860.22949, 5872263908.38623, 
5509868144.98901, 5544641017.91382, 5561244487.76245, 5913443565.36865, 
5541849613.1897, 5286599636.07788, 4948315143.58521, 5221387386.32202, 
5398622512.81738, 5620699882.50732, 5684032917.02271, 5432151794.43359, 
NA, 8491096496.58203, 8830739974.97559, 9183969497.68066, 9551328659.05762, 
9933382034.30176, 10330717086.792, 10743946075.4395, 11173703193.6646, 
11620651245.1172, 12085476875.3052, 12803124427.7954, 11457694053.6499, 
12243959426.8799), informal_economy = c(37.05251, 35.77713, 35.3, 
34.31183, 34.47604, 33.56368, 32.35312, 31.58052, 30.50203, 28.73769, 
27.32484, 28.04083, 27.86167, 26.9567, 27.6698, 27.48514, 27.00871, 
28.22717, 27.7972, 26.96004, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), countrycode = c("ALB", "ALB", 
"ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", 
"ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", "ALB", 
"ALB", "ALB", NA, "DZA", "DZA", "DZA", "DZA", "DZA", "DZA", "DZA", 
"DZA", "DZA", "DZA", "DZA", "DZA", "DZA"), gdp_na_pwt = c(14369190429.6875, 
16221481445.3125, 17348265625, 18787007812.5, 19639292968.75, 
20725078125, 21868023437.5, 23076554687.5, 24438667968.75, 25900890625, 
27843482421.875, 28777429687.5, 29844179687.5, 30603843750, 31037572265.625, 
31348564453.125, 31904841796.875, 32612730468.75, 33693839843.75, 
34974945312.5, 36398882812.5, 37204773437.5, NA, 73350976562.5, 
72145718750, 45074242187.5, 56797289062.5, 62737214843.75, 68657390625, 
64260292968.75, 71811195312.5, 79622679687.5, 86312578125, 94859468750, 
85826265625, 104923804687.5), pop = c(3.110682, 3.122697, 3.129243, 
3.129704, 3.126187, 3.118023, 3.104892, 3.08681, 3.063021, 3.033998, 
3.002678, 2.973048, 2.948023, 2.928592, 2.914096, 2.90379, 2.896305, 
2.890513, 2.886438, 2.884169, 2.88274, 2.880917, NA, 11.3267674450945, 
11.5472444634595, 11.4219286476444, 11.7042645073607, 12.0572548815626, 
12.4208905649919, 12.8113301290637, 13.2488136226961, 13.6493440012352, 
14.0460008378062, 14.464985, 14.87225, 15.28599), gov_pwt = c(0.434667587280273, 
0.381955027580261, 0.330025345087051, 0.318893581628799, 0.301105052232742, 
0.271155476570129, 0.260071277618408, 0.231892690062523, 0.213698744773865, 
0.210174426436424, 0.202188432216644, 0.207810446619987, 0.211970522999763, 
0.205041944980621, 0.193652868270874, 0.201359257102013, 0.202866971492767, 
0.197914361953735, 0.203337371349335, 0.203417494893074, 0.203791037201881, 
0.20607128739357, NA, 0.176263272762299, 0.15668722987175, 0.203446730971336, 
0.22552789747715, 0.209770038723946, 0.200265780091286, 0.237489268183708, 
0.239093273878098, 0.221274048089981, 0.204470127820969, 0.190348103642464, 
0.211000680923462, 0.207922786474228), inv_pwt = c(0.0806355476379395, 
0.102274425327778, 0.148361712694168, 0.189249247312546, 0.212634384632111, 
0.234315633773804, 0.274789750576019, 0.303053259849548, 0.3346888422966, 
0.32933983206749, 0.333941966295242, 0.326972454786301, 0.269711464643478, 
0.265053629875183, 0.244321808218956, 0.242952987551689, 0.239926755428314, 
0.224819242954254, 0.224870756268501, 0.225709408521652, 0.223751932382584, 
0.205827802419662, NA, 0.303861409425735, 0.337309688329697, 
0.372717797756195, 0.359685480594635, 0.272713333368301, 0.265120506286621, 
0.222367390990257, 0.291830718517303, 0.312670230865479, 0.358536124229431, 
0.397604823112488, 0.380427092313766, 0.370294392108917), cgdpo = c(14821.4599609375, 
15896.900390625, 15942.625, 16646.404296875, 17106.9140625, 17991.369140625, 
18591.658203125, 19931.92578125, 21642.23046875, 23265.1171875, 
25638.376953125, 27826.951171875, 30864.365234375, 31103.80859375, 
31867.009765625, 30496.7890625, 31698.853515625, 33465.53515625, 
33650.6171875, 34974.9453125, 35406.609375, 36288.328125, NA, 
119033.09375, 100861.1953125, 52485.65234375, 62272.42578125, 
65276.2890625, 69740.8125, 63045.01953125, 70165.296875, 79194.7109375, 
85981.21875, 99426.671875, 96587.609375, 109762.7421875), gov_pwt_gdp = c(0.448348727279828, 
0.374312361519633, 0.303285090910548, 0.282558645871192, 0.262279210383303, 
0.235389137934808, 0.22110623375499, 0.200292805842645, 0.189245890618609, 
0.188786275022503, 0.18617582248473, 0.200946756325905, 0.219216467307573, 
0.208391647221355, 0.198827981503049, 0.195888101951336, 0.201557194780937, 
0.203089711983364, 0.203076528971459, 0.203417494893074, 0.198235470179188, 
0.200995243436739, NA, 0.286037945977708, 0.219051962734367, 
0.236898811250458, 0.24726830257326, 0.218259763658606, 0.20342599816817, 
0.232997934795992, 0.233613303182996, 0.220084708844678, 0.203685154237364, 
0.199512802383133, 0.237457044163389, 0.217511891363926), inv_pwt_gdp = c(0.0831735473610798, 
0.100227982100483, 0.136340727134841, 0.167686068749482, 0.185216349204734, 
0.203408596928703, 0.233619519159221, 0.26175636549242, 0.296391483756986, 
0.295824954380761, 0.307494941998302, 0.316173008941054, 0.278931210032259, 
0.269383723105577, 0.250850980928416, 0.236351684446254, 0.238377708412466, 
0.230698140595631, 0.224582290737593, 0.225709408521652, 0.217652209480751, 
0.200757756098145, NA, 0.493102686974614, 0.471565866206303, 
0.43400256567785, 0.394358388659629, 0.283750472896879, 0.269304139736818, 
0.218162038491039, 0.285142015989349, 0.310989640757347, 0.35715968166891, 
0.416748320483534, 0.428127020562875, 0.387371845841674), total_oda_gross_gdp_na_pwt = c(0.0264538215886298, 
0.0435946619539087, 0.0292484569332849, 0.0236317568199766, 0.024280914835314, 
0.0224428587045435, 0.0173687393872403, 0.0167226002852598, 0.01607288909945, 
0.0132736748314036, 0.0135460785502779, 0.0133020914013831, 0.0136810595658963, 
0.0131012954867802, 0.0124571598800018, 0.0102196067216744, 0.0108833011055397, 
0.0132270433600567, 0.00966912650831134, 0.00851695398916144, 
0.0128193494949729, 0.0086585663675969, NA, 0.0413372819571996, 
0.055243749304251, 0.0735284685699964, 0.0389918257817378, 0.0275538211937729, 
0.0161346067759912, 0.0149919951418289, 0.0110372762429431, 0.010734253146898, 
0.0106731836774224, 0.00907658467146961, 0.0094944128591171, 
0.0062878009615162), total_oda_gross_ad_gdp_na_pwt = c(0.0196823893025789, 
0.0227587104941443, 0.0217358903852961, 0.0168281188337852, 0.0163921379711711, 
0.0127883715758008, 0.0118360034110879, 0.0120728593922606, 0.0113169015739177, 
0.0100969500928117, 0.0101657542584409, 0.01038105220807, 0.010476414606596, 
0.0102715855749329, 0.00952232982240464, 0.00839910868625925, 
0.00858741133224599, 0.00783467058193221, 0.00707043189807855, 
0.00580215346119421, 0.0100953647916306, 0.00556649001902688, 
NA, 0.0413427351906635, 0.055242640437344, 0.0733066123719842, 
0.0381743571883176, 0.0271532614930819, 0.0159012742847012, 0.00981508130233271, 
0.00655580230841879, 0.00709182361377681, 0.00698878440551737, 
0.00602786424523382, 0.00661405917950889, 0.00388853607830146
), total_oda_net_gdp_na_pwt = c(0.0263807485783487, 0.0432290973154389, 
0.0285019846184192, 0.0226976006107944, 0.0231968635899929, 0.0212583034593507, 
0.0160915320493286, 0.0154191128103165, 0.0146129077270763, 0.0118590516614716, 
0.0120951106222051, 0.0117484432651348, 0.0118361437204438, 0.0112887780640299, 
0.0107959474772165, 0.00810340136563022, 0.00823480027491419, 
0.0102352668789831, 0.00536566923919582, 0.00503817799929548, 
0.00945111424908517, 0.000766837084707083, NA, 0.0438589934417412, 
0.0533180633119689, 0.0722130831719821, 0.038597440761436, 0.0269694790279419, 
0.0156498811012016, 0.0138654830041515, 0.0104661953714781, 0.0101175946748056, 
0.0100037563325884, 0.00848286428970751, 0.00896485469100823, 
0.00567468937838597), total_oda_net_ad_gdp_na_pwt = c(0.0196093162922978, 
0.0223931458556745, 0.0209894180704303, 0.015893962624603, 0.0153080867258499, 
0.0116038163306079, 0.0105587960731762, 0.0107693719173173, 0.00985692020154408, 
0.0086823269228797, 0.00871478633036807, 0.00882740407182169, 
0.00863149876114349, 0.00845906815218268, 0.00786111741961938, 
0.00628290333021504, 0.00593891050162045, 0.00484289410085857, 
0.00276697462896303, 0.00232337747132825, 0.00672712954574284, 
-0.00232523926386294, NA, 0.0438644466752051, 0.0533169544450619, 
0.0719912269739699, 0.0377799721680158, 0.0265689193272509, 0.0154165486099116, 
0.00868856916465535, 0.00598472143695387, 0.00647516514168437, 
0.00631935706068333, 0.00543414386347172, 0.00608450101140003, 
0.00327542449517124), grants_total_gdp_na_pwt = c(0.0160238672545039, 
0.0315260971523522, 0.0198653863993969, 0.0168957187418, 0.0141924661159398, 
0.015684862466592, 0.0109214260119388, 0.0118262887894539, 0.0113136280730828, 
0.00898115834578565, 0.00998797477220425, 0.00880585940967734, 
0.00854203408066114, 0.00780000061266814, 0.00749027655933896, 
0.00601973338467141, 0.00707416139020338, 0.00883857303135674, 
0.00672259383467142, 0.00649007447965541, 0.00759583752677849, 
0.00696738552743673, NA, 0.0413372819571996, 0.0488083016014031, 
0.0642471145261559, 0.0346377447317097, 0.0243125233372063, 0.0146696515965939, 
0.0137062867178075, 0.010710307726435, 0.0100856942161678, 0.00848844995614595, 
0.00684317557913796, 0.00589423291711581, 0.00427224309426327
), oda_grants_ad_gdp_na_pwt = c(0.00925243496845294, 0.0106901456925878, 
0.0123528198514081, 0.0100920807556086, 0.00630368925179691, 
0.0060303753378493, 0.00538869003578641, 0.0071765478964547, 
0.00655764054755056, 0.00580443360719377, 0.00660765048036727, 
0.00588482021636422, 0.00533738912136082, 0.00497029070082087, 
0.00455544650174181, 0.00419923534925624, 0.00477827161690964, 
0.00344620025323221, 0.00412389922443863, 0.00377527395168818, 
0.00487185282343616, 0.00387530917886671, NA, 0.0413427351906635, 
0.0488071927344961, 0.0640252583281437, 0.0338202761382895, 0.0239119636365153, 
0.0144363191053039, 0.00852937287831138, 0.00622883379191071, 
0.00644326468304659, 0.00480405068424087, 0.00379445515290217, 
0.0030138792375076, 0.00187297821104854), ETR_K_gdp_na_pwt = c(0.0303786449940296, 
0.039562451335069, 0.0470120100920642, 0.0527218917353459, 0.0582344752343012, 
0.0604277457136821, 0.0676983010976382, 0.0564467487692829, 0.0552140633789092, 
0.0515584927622052, 0.0463298319009607, 0.0506826953355185, 0.0405290189709006, 
0.0451531849589828, 0.0423007674118299, 0.0374009195335937, 0.0439238548462249, 
0.0481438287691196, 0.0312222911204702, 0.0555801902721304, 0.0537463969586248, 
NA, NA, NA, NA, NA, NA, NA, 0.085161302376715, 0.0841928468236931, 
0.076773862520664, 0.0712959035023866, 0.0766461123328192, 0.0840040021524195, 
0.0967997769837222, 0.0990869787146878), ETR_L_gdp_na_pwt = c(0.0208744949831722, 
0.0225170465016129, 0.0227912592344605, 0.0234836047237048, 0.0243371946717832, 
0.0242992551569075, 0.0259638805418815, 0.0258411321280368, 0.0260720692038592, 
0.0274871834437642, 0.0317175248842792, 0.0325396643790957, 0.0318790358096003, 
0.0291288225553919, 0.0294598674826056, 0.0296429020373745, 0.032497281778682, 
0.0324427760699602, 0.0350025033335024, 0.036794444651957, 0.038076176532942, 
NA, NA, NA, NA, NA, NA, NA, 0.0397725599270224, 0.0370554239832022, 
0.0299210189103173, 0.0233119987050569, 0.0276794797110096, 0.0267687453017037, 
0.024077421292752, 0.0264838603842878), total_ETR_K_gdp_na_pwt = c(206607535.884184, 
284617625.497346, 358153576.393808, 423122225.816064, 480020865.894489, 
523071969.205966, 612787576.587864, 567619121.868568, 596268538.094814, 
600072075.2443, 601650783.249859, 661683857.733425, 581970202.153487, 
644370861.328673, 616472500.876739, 559722407.974054, 661814414.644462, 
723682424.924968, 547027049.716298, 872822916.026403, 884191996.497493, 
NA, NA, NA, NA, NA, NA, NA, 2137797895.33684, 1983020385.24665, 
1997377027.40648, 2076812066.80485, 2422332519.94531, 2856017951.39604, 
2840258668.97211, 3727878614.19142), total_ETR_L_gdp_na_pwt = c(315737096.92993, 
344708085.577812, 395393298.757828, 412507866.953188, 435188900.504701, 
458450568.943588, 510421847.979872, 586352536.024458, 638866519.7918, 
691251927.268383, 805001538.355886, 825119957.193052, 856147151.802916, 
833917644.393408, 831459699.399958, 811153374.559003, 918678151.748526, 
930559749.183552, 991791120.577516, 1057039725.89327, 1108248618.45649, 
NA, NA, NA, NA, NA, NA, NA, 1230397951.43651, 1090633093.60338, 
1023712167.36294, 966311374.289842, 1203908702.03535, 1267710154.84474, 
1046015636.58572, 1408635862.94621), indirect_taxes_gdp_na_pwt = c(0.0503081353345042, 
0.0454451026351894, 0.0507396044788996, 0.047120162456132, 0.0471472132243212, 
0.0473528360812987, 0.0498331450225182, 0.0576423749829067, 0.060294937534012, 
0.0607234536568878, 0.0632235010906127, 0.0616429436845748, 0.0627570199614595, 
0.0613085034178596, 0.0593826253944833, 0.0561003858034954, 0.0631667110203455, 
0.0624372702755391, 0.0625178368712156, 0.0630970904954429, 0.0622504886521908, 
NA, NA, NA, NA, NA, NA, NA, 0.0216676330633622, 0.0206558244346384, 
0.0205787052614075, 0.0209108198632703, 0.0226565730984606, 0.0218299357539366, 
0.0207933960787153, 0.0210476972379957), total_tax_gdp_na_pwt = c(0.101561275311706, 
0.107524600471871, 0.120542873805424, 0.123325658915183, 0.129718883130406, 
0.132079836951888, 0.143495326662038, 0.139930255880226, 0.14158107011678, 
0.139769129862857, 0.141270857875853, 0.144865303399189, 0.13516507474196, 
0.135590510932234, 0.131143260288919, 0.123144207374464, 0.139587847645252, 
0.143023875114619, 0.128742631325188, 0.15547172541953, 0.154073062143758, 
NA, NA, NA, NA, NA, NA, NA, 0.1466014953671, 0.141904095241534, 
0.127273586692389, 0.115518722070714, 0.126982165142289, 0.13260268320806, 
0.14167059435519, 0.146618536336971), inv_priv_imf_gdp_na_pwt = c(0.107801417613723, 
0.111892238686399, 0.175250076161723, 0.201387281547057, 0.20710829781036, 
0.227766593379108, 0.238856150672284, 0.232838413695433, 0.234804526481031, 
0.226720539977039, 0.197887177383395, 0.192673253939779, 0.186342682090597, 
0.193225518130174, 0.178552934674193, 0.168639289495468, 0.155096056425827, 
0.160102736301863, 0.160225802041342, 0.160706466651672, 0.156159543310783, 
0.146006850533817, NA, 0.115759828900806, 0.122401441526641, 
0.203752055541548, 0.168165220853187, 0.158333168902083, 0.15046766258883, 
0.167194165776124, 0.155598345704177, 0.145946497790899, 0.140019880506902, 
0.134969387837684, 0.133498690292689, 0.116693818560494), inv_pub_imf_gdp_na_pwt = c(0.0435327076458244, 
0.0619179482914975, 0.0492153527329441, 0.0529659369213326, 0.0487889684373281, 
0.033845118183643, 0.0383341377998733, 0.0483758844661252, 0.0508366261428332, 
0.0552910217218254, 0.0760354050388119, 0.0749470872789999, 0.0498569120706991, 
0.0508006218967023, 0.0431549029215952, 0.0465319120463858, 0.046708893531429, 
0.0442323669524033, 0.0423541254926329, 0.0462297040380664, 0.0474258224325977, 
0.0450837956073935, NA, 0.0423873303987893, 0.0448192621992297, 
0.0746070955880266, 0.0615764006519708, 0.057976236251926, 0.0550961550023428, 
0.0612208295300512, 0.0569748369052349, 0.0534406571848045, 0.0512705289816475, 
0.0435385125300125, 0.0481470810169449, 0.046677515608227), total_inv_imf_gdp = c(0.151334125259548, 
0.173810186977896, 0.224465428894667, 0.254353218468389, 0.255897266247688, 
0.261611711562751, 0.277190288472157, 0.281214298161558, 0.285641152623865, 
0.282011561698865, 0.273922582422207, 0.267620341218779, 0.236199594161296, 
0.244026140026877, 0.221707837595789, 0.215171201541854, 0.201804949957256, 
0.204335103254267, 0.202579927533975, 0.206936170689738, 0.203585365743381, 
0.191090646141211, NA, 0.158147159299595, 0.16722070372587, 0.278359151129574, 
0.229741621505158, 0.216309405154009, 0.205563817591173, 0.228414995306175, 
0.212573182609412, 0.199387154975704, 0.19129040948855, 0.178507900367696, 
0.181645771309634, 0.163371334168721), log_total_oda_gross_ad_gdp_na_pwt = c(-3.92803098707598, 
-3.7828073280011, -3.82879044987338, -4.08470405160865, -4.11095345109883, 
-4.35921899160668, -4.43660925624821, -4.41679537116201, -4.48145795622465, 
-4.59552187174609, -4.5887306331356, -4.567773037589, -4.55862877631607, 
-4.57837387796788, -4.654115730876, -4.77962968754146, -4.7574579466985, 
-4.84919644848278, -4.95183371210237, -5.14952614392671, -4.59567889199795, 
-5.19099058183999, NA, -3.18585856362104, -2.89602015221077, 
-2.61310446444489, -3.26559126657016, -3.60625811115843, -4.14135602927538, 
-4.62383516781041, -5.02740477285374, -4.94881276223333, -4.96344864264969, 
-5.11136251916926, -5.01855771685009, -5.54972252166698), log_total_tax_gdp_na_pwt = c(-2.28709296500014, 
-2.23003561599586, -2.11574979011782, -2.09292678896007, -2.04238560742895, 
-2.02434871383583, -1.94145281112784, -1.96661115278597, -1.95488279227519, 
-1.9677632899976, -1.95707625347948, -1.9319509103702, -2.00125847165888, 
-1.9981158843003, -2.03146496332318, -2.09439919267604, -1.96906114369482, 
-1.94474370381482, -2.04993997350185, -1.86129139399543, -1.87032835956968, 
NA, NA, NA, NA, NA, NA, NA, -1.92003728926033, -1.95260383517949, 
-2.0614162836169, -2.15832266630607, -2.06370863433551, -2.02039796608021, 
-1.95425067424999, -1.91992105593349), log_priv_inv_imf = c(-2.22746447018811, 
-2.19021902544061, -1.74154131847853, -1.60252545090496, -1.57451344469875, 
-1.47943388762952, -1.43189378828541, -1.45741056945427, -1.44900191318577, 
-1.48403712135136, -1.62005822188667, -1.64675950956348, -1.68016792353228, 
-1.64389728450658, -1.72287016892893, -1.77999322678159, -1.86371063513303, 
-1.83193956792088, -1.83117119588528, -1.82817576652763, -1.85687708081713, 
-1.92410173691008, NA, -2.15623767471109, -2.10044913179483, 
-1.59085143836458, -1.78280832539689, -1.8430538021293, -1.89400708439973, 
-1.78859947262381, -1.86047729901559, -1.92451517792128, -1.96597086283366, 
-2.0027072830148, -2.01366361173161, -2.14820170972581), log_pub_inv_imf = c(-3.13424272351861, 
-2.78194518507687, -3.01154965674298, -2.93810627164743, -3.02025104828981, 
-3.38596050911917, -3.26141445342355, -3.02875384426186, -2.97913819712037, 
-2.89514473947901, -2.57655619139908, -2.59097291737681, -2.9985981348002, 
-2.97984668241157, -3.1429592428416, -3.06761692120421, -3.06382069279345, 
-3.11829847389656, -3.16168944839871, -3.07413274294785, -3.04858842155923, 
-3.09923239620779, NA, -3.16090577274654, -3.10511727216319, 
-2.59551966117458, -2.7874765881458, -2.84772207218818, -2.89867534742979, 
-2.79326779557217, -2.86514556642167, -2.92918345220952, -2.970639175673, 
-3.13410938705714, -3.03349476527667, -3.06449269474046)), row.names = 100:135, class = c("pdata.frame", 
"data.frame"), index = structure(list(country = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("Albania", "Algeria"), class = "factor"), 
    year = structure(c(14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
    22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
    34L, 35L, 36L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 13L), .Label = c("1960", "1961", "1962", "1963", "1964", 
    "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", 
    "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", 
    "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
    "2014", "2015", "2016", "2017", "2018", "2019", "2020"), class = "factor")), row.names = c("100", 
"101", "102", "103", "104", "105", "106", "107", "108", "109", 
"110", "111", "112", "113", "114", "115", "116", "117", "118", 
"119", "120", "121", "122", "123", "124", "125", "126", "127", 
"128", "129", "130", "131", "132", "133", "134", "135"), class = c("pindex", 
"data.frame")))

CodePudding user response:

We may use across

library(dplyr)
MyData %>%  
    group_by(country) %>%
    mutate(across(c(where(is.numeric), -countrycode,-year), 
      ~ .x - lag(.x), .names = "{.col}d")) %>% 
    ungroup 

-output

# A tibble: 36 × 100
   country  year grants_total oda_grants_ad total_oda_gross total_oda_gross_ad oda_total_net total_oda_net_ad region v2x_polyarchy
   <chr>   <dbl>        <dbl>         <dbl>           <dbl>              <dbl>         <dbl>            <dbl> <chr>          <dbl>
 1 Albania  1998         230.          133.            380.               283.          379.             282. Europe         0.401
 2 Albania  1999         511.          173.            707.               369.          701.             363. Europe         0.404
 3 Albania  2000         345.          214.            507.               377.          494.             364. Europe         0.414
 4 Albania  2001         317.          190.            444.               316.          426.             299. Europe         0.443
 5 Albania  2002         279.          124.            477.               322.          456.             301. Europe         0.492
 6 Albania  2003         325.          125.            465.               265.          441.             240. Europe         0.499
 7 Albania  2004         239.          118.            380.               259.          352.             231. Europe         0.5  
 8 Albania  2005         273.          166.            386.               279.          356.             249. Europe         0.522
 9 Albania  2006         276.          160.            393.               277.          357.             241. Europe         0.548
10 Albania  2007         233.          150.            344.               262.          307.             225. Europe         0.551
# … with 26 more rows, and 90 more variables: v2x_libdem <dbl>, v2x_partipdem <dbl>, v2x_delibdem <dbl>, v2x_egaldem <dbl>,
#   corruption <dbl>, dem <dbl>, total_ETR_L <dbl>, total_ETR_K <dbl>, ETR_L <dbl>, ETR_K <dbl>, indirect_taxes <dbl>,
#   total_tax <dbl>, inv_pub_imf <dbl>, inv_priv_imf <dbl>, informal_economy <dbl>, countrycode <chr>, gdp_na_pwt <dbl>,
#   pop <dbl>, gov_pwt <dbl>, inv_pwt <dbl>, cgdpo <dbl>, gov_pwt_gdp <dbl>, inv_pwt_gdp <dbl>, total_oda_gross_gdp_na_pwt <dbl>,
#   total_oda_gross_ad_gdp_na_pwt <dbl>, total_oda_net_gdp_na_pwt <dbl>, total_oda_net_ad_gdp_na_pwt <dbl>,
#   grants_total_gdp_na_pwt <dbl>, oda_grants_ad_gdp_na_pwt <dbl>, ETR_K_gdp_na_pwt <dbl>, ETR_L_gdp_na_pwt <dbl>,
#   total_ETR_K_gdp_na_pwt <dbl>, total_ETR_L_gdp_na_pwt <dbl>, indirect_taxes_gdp_na_pwt <dbl>, total_tax_gdp_na_pwt <dbl>, …
  • Related