increasing the values in a dataset so that they cover the continuum of some numbers

The data is:

df <- structure(list(country = c("Italy", "Italy", "Italy", "Italy", 
                             "Italy", "Italy", "Italy", "Italy", "Italy", "Austria", "Austria", 
                             "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                             "Austria", "Germany", "Germany", "Germany", "Germany", "Germany", 
                             "Germany", "Germany", "Germany", "Germany", "Switzerland", "Switzerland", 
                             "Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland", 
                             "Switzerland", "Switzerland", "Denmark", "Denmark", "Denmark", 
                             "Denmark", "Denmark", "Denmark", "Denmark", "Denmark", "Denmark", 
                             "Norway", "Norway", "Norway", "Norway", "Norway", "Norway", "Norway", 
                             "Norway", "Norway", "Sweden", "Sweden", "Sweden", "Sweden", "Sweden", 
                             "Sweden", "Sweden", "Sweden", "Sweden", "Netherlands", "Netherlands", 
                             "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", 
                             "Netherlands", "Netherlands", "England", "England", "England", 
                             "England", "England", "England", "England", "England", "England", 
                             "France", "France", "France", "France", "France", "France", "France", 
                             "France", "France", "Spain", "Spain", "Spain", "Spain", "Spain", 
                             "Spain", "Spain", "Spain", "Spain", "Portugal", "Portugal", "Portugal", 
                             "Portugal", "Portugal", "Portugal", "Portugal", "Portugal", "Portugal", 
                             "Poland", "Poland", "Poland", "Poland", "Poland", "Poland", "Poland", 
                             "Poland", "Poland", "Russia", "Russia", "Russia", "Russia", "Russia", 
                             "Russia", "Russia", "Russia", "Russia"), date = c(1000, 1200, 
                                                                               1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 
                                                                               1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600, 
                                                                               1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 1750, 
                                                                               1800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 
                                                                               1200, 1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 
                                                                               1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 
                                                                               1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 
                                                                               1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 1750, 1800, 
                                                                               1000, 1200, 1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 
                                                                               1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 
                                                                               1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600, 
                                                                               1700, 1750, 1800), f.0.025quant = c(0.834530947029211, 2.59818762615362, 
                                                                                                                   2.70875565408426, 2.65560708575772, 1.72100046443466, 0.783323670797693, 
                                                                                                                   0.725261888685838, 0.721292570928021, 0.712322038253785, 0.712299216257446, 
                                                                                                                   0.72099839975908, 0.721039256475731, 0.721039470393535, 0.721039471634369, 
                                                                                                                   0.721039470415463, 0.72103925646767, 0.720998399768034, 0.712299216176857, 
                                                                                                                   0.712286296695586, 0.720826693776037, 0.718249193265302, 0.722513648693443, 
                                                                                                                   0.720155354684592, 0.72209416532197, 0.720480290745673, 0.721697953684807, 
                                                                                                                   0.71298404889487, 0.716552227694567, 0.779623019646108, 1.66073191736968, 
                                                                                                                   1.72106061018117, 1.77964369227292, 2.6648989534788, 2.77960584206676, 
                                                                                                                   3.72042869712411, 4.64266949645791, 0.712286412559999, 0.720828122025506, 
                                                                                                                   0.718268813792974, 0.722785947078505, 0.724091719560427, 0.780384625866883, 
                                                                                                                   1.65564430066039, 1.65719549505421, 0.775236623140752, 0.712286296739078, 
                                                                                                                   0.720826693739277, 0.718249193328873, 0.722513648694836, 0.720155354562986, 
                                                                                                                   0.722094165379053, 0.720480291173259, 0.721697953305982, 0.712984048893555, 
                                                                                                                   0.712300892266954, 0.721019452803265, 0.721333297580419, 0.725262300575058, 
                                                                                                                   0.783326153594231, 1.72103946169679, 2.65620506555941, 2.71673629925257, 
                                                                                                                   2.71198093792558, 0.778562332033797, 1.71756477545293, 2.59416512419743, 
                                                                                                                   2.75797663080189, 2.8175626149207, 4.62290865059844, 4.69652573691411, 
                                                                                                                   4.67630763024281, 3.78937543292593, 0.720603621126249, 0.833903974777702, 
                                                                                                                   2.56135099353127, 2.67536492843041, 1.83314660514653, 2.79923135458925, 
                                                                                                                   4.65410930938691, 5.7207835421199, 6.65047805327628, 0.830949351405543, 
                                                                                                                   2.53414238168884, 1.76920676229848, 1.72197097151524, 1.65981126650746, 
                                                                                                                   0.781147035307835, 0.732212178020502, 0.836130351974226, 2.58073083518042, 
                                                                                                                   0.774182430949768, 1.64662130452065, 1.71823817624034, 1.64947801169129, 
                                                                                                                   0.778133230279406, 0.726268408103823, 0.724683364121086, 0.780021048543839, 
                                                                                                                   1.64799301156195, 0.712518743726258, 0.723937566500815, 0.764794995067498, 
                                                                                                                   1.60984003812042, 0.828796734801394, 1.66944681218673, 1.71481049216393, 
                                                                                                                   1.72398534493953, 1.71544301742285, 0.778810381574764, 1.72129245620187, 
                                                                                                                   2.65622841705308, 2.71677794057037, 2.720706150068, 2.72047149149073, 
                                                                                                                   2.71308179703223, 2.59853006291173, 0.834548103831748, 0.712299216227719, 
                                                                                                                   0.720998399730506, 0.721039256462514, 0.721039470358833, 0.721039471564586, 
                                                                                                                   0.721039470391876, 0.721039256440875, 0.720998399769208, 0.712299216214409
                                                                               ), mean = c(1.09411037667825, 2.91151408136954, 2.99169116348787, 
                                                                                           2.95260739731887, 1.99998063966613, 1.04704286468889, 1.00290600736235, 
                                                                                           1.0001991997266, 1.00002162122194, 1.00000719621198, 1.00000564599439, 
                                                                                           1.0000056298405, 1.00000562971764, 1.00000562971671, 1.00000562971689, 
                                                                                           1.00000562984008, 1.00000564599377, 1.00000719621334, 0.999998870739059, 
                                                                                           0.999890488240403, 0.998182083540261, 1.0011539787467, 0.999382243044511, 
                                                                                           1.00080698071002, 0.999605983938524, 1.0005249513932, 1.00050837424598, 
                                                                                           1.00290889921306, 1.04434895587285, 1.95587106618893, 2.00001915013167, 
                                                                                           2.04433620861893, 2.95857607650902, 3.04430933239501, 3.99963109047847, 
                                                                                           4.95004972712353, 0.999998941096736, 0.999891382688346, 0.998194578992864, 
                                                                                           1.00133401702211, 1.00208931385216, 1.04494524676187, 1.95258030784631, 
                                                                                           1.9536671992737, 1.04735744291447, 0.999998870739357, 0.999890488241587, 
                                                                                           0.998182083576737, 1.00115397870821, 0.99938224297301, 1.00080698069996, 
                                                                                           0.999605984230569, 1.00052495107982, 1.00050837434833, 1.00000823184182, 
                                                                                           1.00001911110883, 1.00019912285405, 1.00290613289056, 1.04704465339608, 
                                                                                           2.00000562443454, 2.95296754175137, 2.99710420005782, 2.999800227265, 
                                                                                           1.04977157754569, 1.99766819336615, 2.9118961709809, 3.02868142291692, 
                                                                                           3.07424251329885, 4.92938192720869, 4.98274728897204, 4.96728869154877, 
                                                                                           4.05836901395036, 1.00570098834648, 1.08718599726887, 2.88720147833715, 
                                                                                           2.96698833475984, 2.08652105461075, 3.05943373110879, 4.95109690750067, 
                                                                                           6.00035182965589, 6.95558638205571, 1.09121053634224, 2.86724800007662, 
                                                                                           2.0369015773766, 2.00079563458049, 1.95524559876753, 1.04549645151445, 
                                                                                           1.00774044550731, 1.08900341901092, 2.90640328293427, 1.04657237440911, 
                                                                                           1.94876743417837, 2.00727240958326, 1.94985475502165, 1.04327346381395, 
                                                                                           1.00367699185431, 1.00249184889329, 1.04467465220779, 1.95346763371887, 
                                                                                           1.00015086981532, 1.00203569836558, 1.03335994470747, 1.91938569134555, 
                                                                                           1.08305280505931, 1.96193205074426, 1.99560292390776, 2.00223222904072, 
                                                                                           2.00230223801025, 1.04995219373323, 2.00019912897434, 2.95298094656047, 
                                                                                           2.99710432701573, 2.99978721813928, 2.99963211040207, 2.9945906908647, 
                                                                                           2.91170780752588, 1.09412482417131, 1.00000719620932, 1.00000564599262, 
                                                                                           1.00000562983813, 1.00000562971745, 1.00000562971777, 1.000005629718, 
                                                                                           1.00000562984023, 1.00000564599343, 1.00000719621132), f.0.975quant = c(1.42007591004557, 
                                                                                                                                                                   3.16447153048839, 3.26698442567287, 3.21618194389007, 2.27894380375018, 
                                                                                                                                                                   1.34384206982049, 1.28324704112066, 1.27931886592934, 1.28775382723433, 
                                                                                                                                                                   1.28773096467461, 1.27902401010925, 1.27898307226247, 1.27898285773023, 
                                                                                                                                                                   1.27898285648452, 1.27898285770473, 1.2789830722697, 1.27902401009875, 
                                                                                                                                                                   1.28773096475992, 1.28771804151997, 1.27885466275945, 1.27672392235207, 
                                                                                                                                                                   1.28066668936247, 1.27815924306899, 1.28014066131542, 1.2784487913488, 
                                                                                                                                                                   1.2797676156664, 1.28845639592003, 1.29202487837331, 1.3397041595217, 
                                                                                                                                                                   2.22062904148052, 2.27900402970192, 2.33963876493127, 3.22434181848311, 
                                                                                                                                                                   3.33959267004322, 4.27845488043409, 5.22117255895073, 1.28771815747121, 
                                                                                                                                                                   1.27885609118227, 1.27674334663781, 1.28094111104281, 1.28212147058828, 
                                                                                                                                                                   1.34052570455407, 2.21618268882135, 2.21766518330253, 1.35323027141546, 
                                                                                                                                                                   1.28771804147678, 1.27885466280063, 1.27672392239058, 1.28066668925379, 
                                                                                                                                                                   1.27815924299054, 1.28014066123179, 1.27844879172559, 1.27976761518017, 
                                                                                                                                                                   1.28845639620355, 1.28773264379814, 1.27904509519549, 1.27927779687749, 
                                                                                                                                                                   1.28324703192116, 1.34384529168692, 2.27898284899171, 3.21667514570227, 
                                                                                                                                                                   3.27479547223313, 3.28741262942314, 1.35706623875664, 2.27564221405352, 
                                                                                                                                                                   3.16799616829325, 3.31962745751378, 3.38214756368579, 5.18728515876065, 
                                                                                                                                                                   5.25540671205668, 5.2359159085843, 4.36937990947538, 1.29618157342195, 
                                                                                                                                                                   1.39972016801043, 3.13692316871633, 3.23527711754675, 2.40029632302981, 
                                                                                                                                                                   3.36196406334186, 5.2145042141319, 6.27898084989895, 7.22846724640108, 
                                                                                                                                                                   1.41527600563583, 3.11530046034632, 2.32866407040642, 2.28012145760091, 
                                                                                                                                                                   2.21983652620122, 1.34143518837711, 1.29045334270295, 1.40263720518179, 
                                                                                                                                                                   3.16609839179676, 1.35204729012432, 2.21494506087678, 2.30110476662981, 
                                                                                                                                                                   2.21512657864787, 1.33815353063743, 1.28436008367301, 1.28268701865158, 
                                                                                                                                                                   1.3401602297199, 2.22583188007537, 1.28795097073908, 1.28198396778813, 
                                                                                                                                                                   1.32420511726125, 2.17429149916026, 1.39361784761251, 2.22876052118087, 
                                                                                                                                                                   2.27282123369643, 2.28206047753899, 2.2909242679212, 1.35737245203504, 
                                                                                                                                                                   2.2793187504103, 3.21669365281099, 3.27475489630904, 3.27864958630437, 
                                                                                                                                                                   3.2784154458604, 3.27114143554169, 3.16470757549951, 1.42010367381992, 
                                                                                                                                                                   1.28773096469717, 1.27902401013149, 1.27898307226841, 1.27898285776436, 
                                                                                                                                                                   1.27898285655685, 1.27898285773269, 1.27898307229658, 1.27902401009626, 
                                                                                                                                                                   1.28773096471725)), row.names = c(NA, 126L), class = "data.frame")

Which looks like:

I want to change this data in two ways:

  1. Look at Italy, 1200 (marked as '1' in the pucture). Its 'f.0.025quant', 'mean' and 'f.0.975quant' values are '2.59818762615362', 2.91151408136954' and '3.16447153048839'. They comprise integers 2 and 3. I want to change them so that all three cover the continuum between 2.01-3.99.

  2. Look at Italy, 1000 (marked as '2' in the pucture). Its 'f.0.025quant', 'mean' and 'f.0.975quant' values comprise integers 0 and 1. Whenever they fall below 1.0, I want to change them so that they cover the continuum between 1.01-1.99.

And so should happen for every row in a dataset.

I tried this code:

library(dplyr)
NEWdf <- df |> mutate(scale = f.0.975quant/f.0.025quant / 1.98,
     f.0.025quant_scaled = floor(mean) + 0.01,
     mean_scaled = f.0.025quant_scaled + mean/f.0.025quant/scale,
     f.0.975quant_scaled = f.0.025quant_scaled + f.0.975quant/f.0.025quant/scale)

The output is:

However, there are 3 problems with this code:

  1. Look at 'scaled' values of Italy, 1000 (marked as '1' in the pucture). They cover 1.01-2.99, instead of supposed 1.01.-1.99.

  2. Look at 'scaled' values of Germany, 1000 (marked as '2' in the pucture). They cover 0.01.-1.99., instead of supposed 1.01.-1.99.

  3. Look at 'scaled' values of Switzerland, 1400. They cover 2.01-3.99, instead of supposed 1.01.-2.99.

Guys, can you help me out to fix this code?

Hi @Rustam ,

i will post my approach but i have to admit that i don't fully get your explanation

NEWdf <- df |> 
  rowwise() |>
  mutate(tempFrom = min(floor(c(f.0.025quant, mean, f.0.975quant))),
         tempTo = max(floor(c(f.0.025quant, mean, f.0.975quant))),
         temp = list(setNames(
                          object = scales::rescale(x = c(f.0.025quant, mean, f.0.975quant), 
                                                   to = c(ifelse(tempFrom <= 1, 1.01, tempFrom + 0.01), 
                                                          ifelse(tempTo < 1, 1.99, tempTo+0.99))
                                                   ),
                          nm = c("f.0.025quant_scaled", "mean_scaled", "f.0.097quant_scaled"))
         )
  )|>
 tidyr::unnest_wider(temp)

Uses the rescale function from the scales package and unnest_wider from tidyr.

tempFrom and tempTo are left as reference.

Hi @vedoa ,

Thank you for your awesome code!

It works great!

Sincerely,
Rustam

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.