My question is rather problematic since I know the code is completely fine. I leave you below what I am doing (very simple stuff):
Means_region <- data %>%
mutate(year_fctr = case_when(edate < "1994-12-31" ~ "1990-1994",
edate > "1995-01-01" & edate < "1999-12-31" ~ "1995-1999",
edate > "2000-01-01" & edate < "2004-12-31" ~ "2000-2004",
edate > "2005-01-01" & edate < "2009-12-31" ~ "2005-2009",
edate > "2010-01-01" & edate < "2014-12-31" ~ "2010-2014",
edate > "2015-01-01" & edate < "2019-12-31" ~ "2015-2019",
edate > "2020-01-01" ~ "2020-2022")
) %>%
group_by(region, year_fctr) %>%
summarise(pol=mean(pol))
The issue is that even if the code creates perfectly fine the column year_fctr
using mutate()
and case_when()
(I have checked it just using the mutate and the column exists), it just will not group it based on that existing column. The worst part of all of this is that it did work the day before yesterday and yesterday loading again the file and the code it won't work.
I have reset RStudio, cleaning and reloading all the objects in the environment and even resetting my computer and it will not cooperate, it just won't.
dput()
from my dataframe of the beginning (taking the first ten rows head(data,10)
:
structure(list(countryname = c("Sweden", "Sweden", "Sweden",
"Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "Norway"
), edate = structure(c(7927, 9026, 10490, 11945, 13408, 14871,
16327, 17783, 19246, 8656), class = "Date"), date = c(199109L,
199409L, 199809L, 200209L, 200609L, 201009L, 201409L, 201809L,
202209L, 199309L), pervote = c(3.383, 5.023, 4.5, 4.646, 5.244,
7.34, 6.889, 4.414, 5.08262535054372, 7.907), per108_bal = c(0,
0, 0, 0, 0, 0, 0, 0.00255657175901268, 0, 0), per110_bal = c(0.153842292827385,
0.0862068965517241, 0.127269633062352, 0.0673106693912936, 0.0180167323712517,
0.0128146991148956, 0.00659394734683835, 0, 0, 0.0599817523618919
), per107_bal = c(0.0769295119543576, 0, 0.0909031405045229,
0.125007740417363, 0.0360334647425034, 0, 0.0230952185680308,
0.0127828587950634, 0.0540516084274662, 0.166288960178945), per109_bal = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0.00910910321688201), per104_bal = c(0,
0, 0, 0, 0, 0, 0, 0.00511314351802535, 0.0270258042137331, 0.0113900579803985
), per105_bal = c(0.0769295119543576, 0.0344827586206897, 0.072732985115658,
0.0480834726608459, 0.0180167323712517, 0.0320451453620194, 0.00990732387435414,
0.0255809352791685, 0.0432382704691812, 0.0242958471907467),
per504_bal = c(0, 0.0689655172413793, 0.127269633062352,
0.182689330608706, 0.297311760823418, 0.198720209603467,
0.181513983433117, 0.163681463332978, 0.162169906646357,
0.209568237337022), per505_bal = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0.00303146245989934), per506_bal = c(0, 0, 0.0181832462789145,
0.0384543934608954, 0.0360334647425034, 0.166675064241447,
0.250832444845403, 0.0920670187025398, 0.0540516084274662,
0.0698560791123407), per507_bal = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), per203_bal = c(0, 0.0172413793103448, 0, 0, 0,
0, 0, 0, 0.0108133379582849, 0), per204_bal = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), per301_bal = c(0, 0, 0, 0.0288407950956716,
0.0540501971137551, 0.0192304462471238, 0.00990732387435414,
0, 0, 0.0174676987373812), per302_bal = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0.00379668599346617), per603_bal = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0.0280925331842129), per604_bal = c(0,
0, 0, 0, 0, 0, 0.00659394734683835, 0.00255657175901268,
0.0162124662554481, 0.00379668599346617), per607_bal = c(0,
0, 0, 0, 0, 0.00641574713222821, 0.0462068399901583, 0.0306940787971939,
0.0162124662554481, 0.0470759631515437), per608_bal = c(0,
0, 0, 0, 0, 0, 0, 0.00766971527703803, 0, 0.00910910321688201
), per601_bal = c(0, 0, 0, 0, 0, 0, 0.00990732387435414,
0.0102262870360507, 0, 0), per602_bal = c(0, 0, 0, 0, 0,
0, 0.0429098663167391, 0.0434769375922573, 0, 0), per406_bal = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0.00456190952703299), per407_bal = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0.000765223533566825), per401_bal = c(0,
0, 0, 0, 0, 0, 0.00990732387435414, 0.00255657175901268,
0.00539912829716319, 0), per402_bal = c(0, 0, 0, 0.0673106693912936,
0.063058563299381, 0.0961522312356192, 0.0230952185680308,
0.0204677917611432, 0, 0.0220148924272302), per414_bal = c(0.0192240124479245,
0.172413793103448, 0.036366492557829, 0, 0, 0, 0, 0, 0, 0.00303146245989934
), per403_bal = c(0.0192240124479245, 0.137931034482759,
0.109086386783437, 0.00961359836522385, 0.0180167323712517,
0.0192304462471238, 0.00329697367341918, 0, 0.00539912829716319,
0.000765223533566825), per404_bal = c(0, 0, 0, 0, 0, 0.00641574713222821,
0.00329697367341918, 0, 0, 0), per405_bal = c(0, 0, 0, 0,
0.0180167323712517, 0, 0, 0, 0, 0), per409_bal = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0.000765223533566825), per412_bal = c(0,
0, 0, 0, 0.063058563299381, 0, 0, 0, 0, 0.022780115960797
), per413_bal = c(0, 0, 0, 0, 0, 0, 0.00659394734683835,
0, 0, 0.00303146245989934), per415_bal = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0.00303146245989934), per410_bal = c(0.0192240124479245,
0.0172413793103448, 0.0181832462789145, 0, 0, 0, 0, 0, 0.0162124662554481,
0.0220148924272302), per416_bal = c(0, 0, 0, 0, 0, 0, 0.0627081112113508,
0.166238035091991, 0.227034853032108, 0.077449451099273),
per501_bal = c(0.634626645920126, 0.46551724137931, 0.40000523635602,
0.432689330608706, 0.306320127009044, 0.391024672074705,
0.231034199950791, 0.383637940742319, 0.237848190990393,
0.164022721252612), per605_bal = c(0, 0, 0, 0, 0.0720669294850069,
0.0512755916091433, 0.0693020585581891, 0.0306940787971939,
0.124330764474339, 0.0129057892103482), per605_1_bal = c(NA,
NA, NA, NA, NA, NA, 0.0693020585581891, 0.0306940787971939,
0.124330764474339, NA), per605_2_bal = c(NA, NA, NA, NA,
NA, NA, 0.00329697367341918, 0, 0, NA), region = c("Northern",
"Northern", "Northern", "Northern", "Northern", "Northern",
"Northern", "Northern", "Northern", "Northern"), year = c("1991",
"1994", "1998", "2002", "2006", "2010", "2014", "2018", "2022",
"1993"), pos_euro = c(-0.153842292827385, -0.0862068965517241,
-0.127269633062352, -0.0673106693912936, -0.0180167323712517,
-0.0128146991148956, -0.00659394734683835, 0.00255657175901268,
0, -0.0599817523618919), pos_inter = c(0.0769295119543576,
0, 0.0909031405045229, 0.125007740417363, 0.0360334647425034,
0, 0.0230952185680308, 0.0127828587950634, 0.0540516084274662,
0.157179856962063), pos_def = c(-0.0769295119543576, -0.0344827586206897,
-0.072732985115658, -0.0480834726608459, -0.0180167323712517,
-0.0320451453620194, -0.00990732387435414, -0.0204677917611432,
-0.0162124662554481, -0.0129057892103482), pos_welf = c(0,
0.0689655172413793, 0.127269633062352, 0.182689330608706,
0.297311760823418, 0.198720209603467, 0.181513983433117,
0.163681463332978, 0.162169906646357, 0.206536774877123),
pos_edu = c(0, 0, 0.0181832462789145, 0.0384543934608954,
0.0360334647425034, 0.166675064241447, 0.250832444845403,
0.0920670187025398, 0.0540516084274662, 0.0698560791123407
), pos_const = c(0, 0.0172413793103448, 0, 0, 0, 0, 0, 0,
0.0108133379582849, 0), pos_reg = c(0, 0, 0, 0.0288407950956716,
0.0540501971137551, 0.0192304462471238, 0.00990732387435414,
0, 0, 0.013671012743915), pos_tradlib = c(0, 0, 0, 0, 0,
0, -0.00659394734683835, -0.00255657175901268, -0.0162124662554481,
0.0242958471907467), pos_multicul = c(0, 0, 0, 0, 0, 0.00641574713222821,
0.0462068399901583, 0.0230243635201558, 0.0162124662554481,
0.0379668599346617), pos_nat = c(0, 0, 0, 0, 0, 0, -0.033002542442385,
-0.0332506505562065, 0, 0), pos_prtct = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0.00379668599346617), pos_laissezplan = c(0,
0.0344827586206897, -0.0727198942256084, 0.0576970710260697,
-0.0360334647425034, 0.0705060378562671, 0.0198146477487083,
0.0230243635201558, 0, -0.00532713306059982), pos_env = c(-0.615402633472201,
-0.448275862068966, -0.381821990077105, -0.432689330608706,
-0.306320127009044, -0.391024672074705, -0.293742311162142,
-0.54987597583431, -0.448670577767053, -0.219457279924655
), pos_secur = c(0, 0, 0, 0, 0.0720669294850069, 0.0512755916091433,
0.0660050848847699, 0.0306940787971939, 0.124330764474339,
0.0129057892103482), sal_euro = c(0.153842292827385, 0.0862068965517241,
0.127269633062352, 0.0673106693912936, 0.0180167323712517,
0.0128146991148956, 0.00659394734683835, 0.00255657175901268,
0, 0.0599817523618919), sal_inter = c(0.0769295119543576,
0, 0.0909031405045229, 0.125007740417363, 0.0360334647425034,
0, 0.0230952185680308, 0.0127828587950634, 0.0540516084274662,
0.175398063395827), sal_def = c(0.0769295119543576, 0.0344827586206897,
0.072732985115658, 0.0480834726608459, 0.0180167323712517,
0.0320451453620194, 0.00990732387435414, 0.0306940787971939,
0.0702640746829143, 0.0356859051711452), sal_welf = c(0,
0.0689655172413793, 0.127269633062352, 0.182689330608706,
0.297311760823418, 0.198720209603467, 0.181513983433117,
0.163681463332978, 0.162169906646357, 0.212599699796921),
sal_edu = c(0, 0, 0.0181832462789145, 0.0384543934608954,
0.0360334647425034, 0.166675064241447, 0.250832444845403,
0.0920670187025398, 0.0540516084274662, 0.0698560791123407
), sal_const = c(0, 0.0172413793103448, 0, 0, 0, 0, 0, 0,
0.0108133379582849, 0), sal_reg = c(0, 0, 0, 0.0288407950956716,
0.0540501971137551, 0.0192304462471238, 0.00990732387435414,
0, 0, 0.0212643847308473), sal_tradlib = c(0, 0, 0, 0, 0,
0, 0.00659394734683835, 0.00255657175901268, 0.0162124662554481,
0.031889219177679), sal_multicul = c(0, 0, 0, 0, 0, 0.00641574713222821,
0.0462068399901583, 0.0383637940742319, 0.0162124662554481,
0.0561850663684257), sal_nat = c(0, 0, 0, 0, 0, 0, 0.0528171901910933,
0.053703224628308, 0, 0), sal_prtct = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0.00532713306059982), sal_laissezplan = c(0.038448024895849,
0.310344827586207, 0.145452879341266, 0.0769242677565174,
0.162150591341265, 0.121798424614971, 0.0461904371360617,
0.0230243635201558, 0.0107982565943264, 0.0554198428348589
), sal_env = c(0.65385065836805, 0.482758620689655, 0.418188482634934,
0.432689330608706, 0.306320127009044, 0.391024672074705,
0.293742311162142, 0.54987597583431, 0.48109551027795, 0.263487064779115
), sal_secur = c(0, 0, 0, 0, 0.0720669294850069, 0.0512755916091433,
0.0725990322316083, 0.0306940787971939, 0.124330764474339,
0.0129057892103482), mean_pos_euro = c(0.0404361358040147,
0.0480363461480908, 0.0170220063948794, 0.018727122564046,
0.00538808219373917, 0.00658559488995673, 0.00350714652845766,
0.0101128077847298, 0.00373485709176495, 0.0105472299278959
), mean_pos_inter = c(0.101206010823922, 0.0654061309668344,
0.0573142114021365, 0.0996268998338684, 0.0308219362264086,
0.0273638026244262, 0.0253366544457652, 0.025677921475989,
0.020053434775811, 0.113160207665325), mean_pos_def = c(-0.00445667220872397,
-0.00691313408409361, -0.0111580658887506, -0.00266497640598704,
0.0033722213429837, 0.00517196450244125, 0.0230884629829038,
0.0223213891670414, 0.0337724951671837, 0.0170737655398906
), mean_pos_welf = c(0.0955760782947446, 0.0491786352684619,
0.134720127683514, 0.139873786512013, 0.245743924029886,
0.270878654847156, 0.221332473888756, 0.216870326707365,
0.23037102643583, 0.110456681118002), mean_pos_edu = c(0.0422358974915196,
0.0336899136392136, 0.0900154092134119, 0.1068205673599,
0.117690762687617, 0.140876796501983, 0.157814699216243,
0.107806156963104, 0.084771692956725, 0.0794119363921141),
mean_pos_const = c(0.00240358912214421, 0.00246305418719212,
0, 0, 0, 0, 0, -0.000749739991613986, 0.00388068444820163,
0.00163555090314125), mean_pos_reg = c(0.0141917777345324,
0.0111002690880246, 0.0477885018709644, 0.0218850827825929,
0.0271937450200291, 0.0149780111154722, 0.0021732084523248,
0.000434175375494105, 0.00618337848305643, 0.0472509802244062
), mean_pos_tradlib = c(0.0345396003140203, 0.0378114798624833,
0.0241161588961318, 0.0465901079299324, 0.0252142439253487,
0.0325166258575727, -0.00327025895660341, 0.0089414255126401,
-0.00650404779335592, 0.0609923715866183), mean_pos_multicul = c(0,
0, 0, 0.0030693494666013, 0.00544836804447179, -0.00284347951581304,
0.00997918219120811, -0.0283611122579275, -0.0216871883800512,
0.0163256903737892), mean_pos_nat = c(0.0228146034646482,
0.00948545000965647, 0, 0.00119712780575803, 0.0173747891724639,
0.0233458495989161, -0.0170253758785428, 0.0267910060744619,
0.0221192500306537, 0.0104022961990873), mean_pos_prtct = c(0,
0, 0, -0.00982979796123083, -0.00275662391361668, -0.00273383656086004,
-0.00803142055243403, -0.00276918899400266, -0.00717491644512924,
-0.00618615955878603), mean_pos_laissezplan = c(0.198961558244306,
0.264756681086296, 0.2114493593512, 0.142303569931153, -0.00123888931418084,
0.064055787813, 0.0525403476282539, 0.0253875999395252, 0.0217444443508756,
0.0923980577133026), mean_pos_env = c(-0.222320379650192,
-0.128394356251492, -0.120356559470918, -0.173768791906174,
-0.123231884978827, -0.18090793470031, -0.182462617948571,
-0.173369283883448, -0.167809266855446, -0.15402820754184
), mean_pos_secur = c(0.0421803173790586, 0.0179864617446783,
0.0496980555581064, 0.0658599594917766, 0.0830105489614746,
0.0640785618856314, 0.0790176341169412, 0.129025624283268,
0.196959628657995, 0.0184279910294444), mean_sal_euro = c(0.0929834745456625,
0.103812462872433, 0.0783131820482527, 0.055091807735765,
0.0249043301273878, 0.0239243670761762, 0.014006300998206,
0.022840333059475, 0.00952830966914289, 0.0474607561478726
), mean_sal_inter = c(0.101206010823922, 0.0654061309668344,
0.0573142114021365, 0.101613325922264, 0.0308219362264086,
0.0273638026244262, 0.037849230867531, 0.0311806956484329,
0.0222237487191981, 0.125568694029407), mean_sal_def = c(0.0218166589567979,
0.0129087526705112, 0.0111580658887506, 0.0110731586399689,
0.0135107902048928, 0.0231164198966239, 0.0352171325654738,
0.0450392592385245, 0.0501110731670185, 0.0428230907546493
), mean_sal_welf = c(0.0955760782947446, 0.0491786352684619,
0.134720127683514, 0.143400202300806, 0.267992434331611,
0.281039884521543, 0.255387581523303, 0.251394862175024,
0.253662039372808, 0.14009330065814), mean_sal_edu = c(0.0422358974915196,
0.0336899136392136, 0.0900154092134119, 0.1068205673599,
0.117690762687617, 0.140876796501983, 0.158919290412965,
0.108502477312454, 0.084771692956725, 0.0833169730518998),
mean_sal_const = c(0.00240358912214421, 0.00246305418719212,
0, 0, 0, 0, 0, 0.0016055647422127, 0.00388068444820163, 0.00163555090314125
), mean_sal_reg = c(0.0141917777345324, 0.0111002690880246,
0.0477885018709644, 0.0218850827825929, 0.0287786790489457,
0.0188495752977156, 0.00603244115101005, 0.00214582487669153,
0.0138830874939166, 0.0566212837555651), mean_sal_tradlib = c(0.0345396003140203,
0.0378114798624833, 0.0241161588961318, 0.0465901079299324,
0.0260043167796425, 0.0352067066965501, 0.0131793254621516,
0.0237704951987535, 0.0158366235669746, 0.0758056528011791
), mean_sal_multicul = c(0, 0, 0, 0.0030693494666013, 0.00780742440395134,
0.0203050767295434, 0.0289669020505296, 0.0591820122496133,
0.0286632440086366, 0.0328690285600878), mean_sal_nat = c(0.0228146034646482,
0.00948545000965647, 0, 0.00119712780575803, 0.0173747891724639,
0.0233458495989161, 0.0588822580544644, 0.0818606797934299,
0.0253205545178215, 0.0104022961990873), mean_sal_prtct = c(0,
0, 0, 0.011087604405295, 0.00435244621228382, 0.00428831784578114,
0.00930435686378787, 0.00276918899400266, 0.00743248554233238,
0.010448883967899), mean_sal_laissezplan = c(0.235597333841816,
0.375038470455887, 0.300404143280935, 0.224174950154609,
0.209990306577062, 0.120357753198566, 0.103260624967218,
0.0559154639642559, 0.099564128416099, 0.130279841045087),
mean_sal_env = c(0.294454658031133, 0.281118919234624, 0.206472144157797,
0.208136756004731, 0.167761235266259, 0.217246888126545,
0.198188921508806, 0.181185583122314, 0.187202114994835,
0.22424665709654), mean_sal_secur = c(0.0421803173790586,
0.0179864617446783, 0.0496980555581064, 0.0658599594917766,
0.0830105489614746, 0.0640785618856314, 0.0808056335745532,
0.132607559624816, 0.197920213126291, 0.0184279910294444),
pol_euro = c(0.12082489733415, 0.101965194780124, 0.0789706069518755,
0.0445902709604092, 0.0194661714613817, 0.0186334904089752,
0.00988765784240235, 0.0230218323107435, 0.00429075497853459,
0.0399389113028663), pol_inter = c(0.0627844821677291, 0.0274692510352796,
0.0245832042993927, 0.0345255070743195, 0.00478596997674972,
0.00670403084654029, 0.0100368732784001, 0.0298513240469037,
0.00867186165955998, 0.0540250286881538), pol_def = c(0.0233471906735174,
0.00835828879721331, 0.0166115858614565, 0.0104227523902427,
0.00638846514274101, 0.0150156538983498, 0.0247107105963671,
0.0279414578055665, 0.0244812265026666, 0.0241485506686932
), pol_welf = c(0.0873439168968685, 0.0289698507603164, 0.0737381610198812,
0.0656647659070997, 0.118565103422695, 0.183331726941973,
0.20267625838291, 0.154279049072952, 0.184420159686011, 0.0664614599508951
), pol_edu = c(0.0272386235213555, 0.0156098663404123, 0.0285593096411532,
0.0695012410765138, 0.0510672344551977, 0.073444467233151,
0.103694834608818, 0.0506619712529666, 0.0408264054704871,
0.0177661546644044), pol_const = c(0.00233310013238297, 0.0019404891555624,
0, 0, 0, 0, 0, 0.000580012304622843, 0.00117040053889352,
0.000548475208106062), pol_reg = c(0.0104756330923493, 0.0158518573891979,
0.0615191396223416, 0.0192176080247559, 0.0129369368751246,
0.0122073410020887, 0.00341459147399128, 0.000830166568196308,
0.00843729903346307, 0.0297634634964584), pol_tradlib = c(0.0404150455448279,
0.072633038703069, 0.0200087486925339, 0.0874443251250441,
0.025899817803368, 0.0460033405697498, 0.00710777744698461,
0.0307542736151476, 0.0053468007590204, 0.0985277278897992
), pol_multicul = c(0, 0, 0, 0.00146071281424595, 0.00252879105290296,
0.0278248778471243, 0.018152783138818, 0.0254026954454675,
0.0142637698583309, 0.0102787819217894), pol_nat = c(0.0229596623992457,
0.00798290787216119, 0, 0.000274215166375891, 0.00742174780607839,
0.0515017483218547, 0.0598085365548079, 0.0555354323081328,
0.0205069527159616, 0.00310324796383001), pol_prtct = c(0,
0, 0, 0.00354527572444212, 0.00189796674798386, 0.0014556343814613,
0.00716639125443205, 0.000700674855103647, 0.00394873049434092,
0.00566911635265743), pol_laissezplan = c(0.278288813717876,
0.376361953696313, 0.357479936633271, 0.287281480937095,
0.207993954574261, 0.0623753184129893, 0.0778456285270103,
0.0361660789093771, 0.100237506868417, 0.173059122895247),
pol_env = c(0.332405632544831, 0.317219856397167, 0.195211513438994,
0.183434433562485, 0.111600537832815, 0.210121405966822,
0.195233819217862, 0.289261827141532, 0.204595055622314,
0.0986332280356917), pol_secur = c(0.041078200596562, 0.0141767188530247,
0.0257711735095036, 0.0324405769403972, 0.0731944691103037,
0.0409221644437707, 0.0382844246976587, 0.0669307041862785,
0.156172746603598, 0.00220077819367665), pol = c(0.0139175622804025,
0.0177160664645774, 0.0123343546293873, 0.00944119716090404,
0.00772721088558479, 0.00873197265152529, 0.0088344526144641,
0.00836357606968884, 0.00943256631379078, 0.00534241718974811
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), groups = structure(list(countryname = c("Norway",
"Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden",
"Sweden", "Sweden"), edate = structure(c(8656, 7927, 9026, 10490,
11945, 13408, 14871, 16327, 17783, 19246), class = "Date"), .rows = structure(list(
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))
dput()
is the following for the dataframe not working:
structure(list(pol = 0.00849904532770635), class = "data.frame", row.names = c(NA, -1L))
Please if you know how to get it working, specially any debugging tools so as to fix this absurd issue, I would greatly appreciate it. Thanks.
Update: I have applied @RuiBarradas solution to add the .by
parameter in summarise, yet a new error has appeared.
Running traceback()
as @yuk recommended gives the following output:
5: stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))
4: stopifnot(length(rows) == 1)
3: quickdf(.data[names(cols)])
2: summarise(., pol = mean(pol), .by = c(region, year_fctr))
1: data %>% ungroup() %>% mutate(year_fctr = case_when(edate < "1994-12-31" ~
"1990-1994", edate > "1995-01-01" & edate < "1999-12-31" ~
"1995-1999", edate > "2000-01-01" & edate < "2004-12-31" ~
"2000-2004", edate > "2005-01-01" & edate < "2009-12-31" ~
"2005-2009", edate > "2010-01-01" & edate < "2014-12-31" ~
"2010-2014", edate > "2015-01-01" & edate < "2019-12-31" ~
"2015-2019", edate > "2020-01-01" ~ "2020-2022")) %>% summarise(pol = mean(pol),
.by = c(region, year_fctr))
Like I say in my comment to the question, the problem seems to come from the fact that the data is already grouped.
The code below starts by ungrouping and then summarise
with the argument .by
to group/compute the summary statistics/ungroup all in the same instruction.
source("~/so.txt")
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data %>%
ungroup() %>%
mutate(year_fctr = case_when(edate < "1994-12-31" ~ "1990-1994",
edate > "1995-01-01" & edate < "1999-12-31" ~ "1995-1999",
edate > "2000-01-01" & edate < "2004-12-31" ~ "2000-2004",
edate > "2005-01-01" & edate < "2009-12-31" ~ "2005-2009",
edate > "2010-01-01" & edate < "2014-12-31" ~ "2010-2014",
edate > "2015-01-01" & edate < "2019-12-31" ~ "2015-2019",
edate > "2020-01-01" ~ "2020-2022")
) %>%
summarise(pol = mean(pol), .by = c(region, year_fctr))
#> # A tibble: 7 × 3
#> region year_fctr pol
#> <chr> <chr> <dbl>
#> 1 Northern 1990-1994 0.0123
#> 2 Northern 1995-1999 0.0123
#> 3 Northern 2000-2004 0.00944
#> 4 Northern 2005-2009 0.00773
#> 5 Northern 2010-2014 0.00878
#> 6 Northern 2015-2019 0.00836
#> 7 Northern 2020-2022 0.00943
Created on 2024-12-18 with reprex v2.1.1