将数据集中的值增加到当前包含的最高数量

问题描述 投票:0回答:1

数据是

structure(list(country = c("Italy", "Italy", "Italy", "Italy", 
                       "Italy", "Italy", "Italy", "Italy", "Italy", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", 
                       "Spain", "Spain", "Spain", "Portugal", "Portugal", "Portugal", 
                       "Portugal", "Portugal", "Portugal", "Portugal", "Portugal", "Portugal"), date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 
                  2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                  9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), levels = c("1000", "1200", 
                                                                      "1300", "1400", "1500", "1600", "1700", "1750", "1800"), class = "factor"), fit.0.025quant = c(0.828858435568409, 2.49016922773755, 2.65828836880409, 
               2.66581093505707, 2.57548603944065, 1.67885386266028, 0.767937801519583, 
               0.689014091834041, 0.66513695912892, 0.663699022639656, 0.678689217926065, 
               0.67888578207667, 0.678889008309296, 0.678889067277956, 0.678889008315121, 
               0.678885782094281, 0.67868921794039, 0.663699022695475, 0.753024027011781, 
               1.56176460225556, 1.66925563033888, 1.56663639224176, 0.757961319722877, 
               0.690760522079772, 0.688181548019768, 0.760878851337501, 
               1.5613478080279, 0.664563945566996, 0.68508607246818, 0.737286017774939, 
               1.5119543712026, 0.819687233979314, 1.60507738574301, 1.66679441254118, 
               1.68306812289583, 1.66919593888341), fit.mean = c(1.13400448293661, 
                                                                 2.87830024613312, 2.98840123437886, 2.99270845416986, 2.93292120022681, 
                                                                 2.00001375693309, 1.06700462995692, 1.00618961591289, 1.0007893132799, 
                                                                 1.00004363941275, 1.00003146052876, 1.00003107297182, 1.00003106386579, 
                                                                 1.00003106366991, 1.00003106386888, 1.00003107297307, 1.00003146052807, 
                                                                 1.00004363941059, 1.06578800415204, 1.92791273808794, 2.00911809484502, 
                                                                 1.92938242492218, 1.05953000948759, 1.00731995863015, 1.00551539597356, 
                                                                 1.0617929411748, 1.93397100841674, 1.00049922917461, 1.00391214501682, 
                                                                 1.04414180746079, 1.89014845620677, 1.1133844912819, 1.94890869596221, 
                                                                 1.99253538304467, 2.00313100995317, 2.00364556256633), fit.0.975quant = c(1.5548453965449, 
                                                                                                                                           3.17193689185671, 3.30217150519806, 3.30887034452248, 3.23188378430461, 
                                                                                                                                           2.32122877803568, 1.42445510409649, 1.33229333739629, 1.33797676580827, 
                                                                                                                                           1.33651739528035, 1.32146593655336, 1.32126741860939, 1.32126414685111, 
                                                                                                                                           1.32126408690283, 1.32126414685902, 1.32126741859377, 1.32146593653428, 
                                                                                                                                           1.33651739521255, 1.43806634680653, 2.23154393918938, 2.35542858727393, 
                                                                                                                                           2.23131692404623, 1.40982833868788, 1.33388203589275, 1.33103300867831, 
                                                                                                                                           1.41382643372889, 2.24644777498799, 1.33739126255841, 1.32804123237336, 
                                                                                                                                           1.38436190848754, 2.18404788457664, 1.4941065137638, 2.25306205937895, 
                                                                                                                                           2.30955823803133, 2.32596318832388, 2.34217586698821)), row.names = c(1L, 
                                                                                                                                                                                                                 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
                                                                                                                                                                                                                 16L, 17L, 18L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 
                                                                                                                                                                                                                 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L), class = "data.frame")

问题如下:

对于日期为“1000”的意大利,“fit.0.025quant”、“fit.mean”和“fit.0.975quant”的值为

0.6736376
1.0000012
1.326401
。我希望所有三个值都提升 0.4 个值,以便它们完全位于 1.01-1.99 之间。

数据集中的每一行都应该如此。例如,对于日期“1200”的意大利,“fit.0.025quant”、“fit.mean”和“fit.0.975quant”的值为 2.5193345、2.8869927 和 3.168365。我希望所有三个值都提升 0.5,以便它们完全位于 3.01-3.99 之间。

因此,这就像将‘fit.0.025quant’、fit.mean’和‘fit.0.975quant’提升到当前位于其边界内的最高数字,以便所有三个都完全位于其边界内。

如果“fit.0.025quant”、“fit.mean”和“fit.0.975quant”的值已经完全位于某个数字的边界内(例如 5.01-5.99),那么这些值应该不会发生任何情况。

伙计们,我希望我的解释不会令人困惑。

您认为有合适的代码可以按预期自动转换整个数据集中的值吗?

r dataframe
1个回答
0
投票

使用

apply()
查找每行的最小值和最大值下限之间的差异:

add <- apply(dat[, 3:5], 1, \(x) floor(max(x))) - apply(dat[, 3:5], 1, min)
add <- ifelse(add < -0.01, 0, add + 0.01)
dat[, 3:5] <- dat[, 3:5] + add

结果:

#> head(dat, 10)
   country date fit.0.025quant fit.mean fit.0.975quant
1    Italy 1000           1.01 1.315146       1.735987
2    Italy 1200           3.01 3.398131       3.691768
3    Italy 1300           3.01 3.340113       3.653883
4    Italy 1400           3.01 3.336898       3.653059
5    Italy 1500           3.01 3.367435       3.666398
6    Italy 1600           2.01 2.331160       2.652375
7    Italy 1700           1.01 1.309067       1.666517
8    Italy 1750           1.01 1.327176       1.653279
9    Italy 1800           1.01 1.345652       1.682840
10 Austria 1000           1.01 1.346345       1.682818
© www.soinside.com 2019 - 2024. All rights reserved.