r 计算 2 个连续值的百分比变化
r calculate percent chnage to 2 succesive values
我正在尝试查找 2 个连续值的百分比变化。
df = data.frame(k = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),
rmse = c(61.41,58.7,56.84,55.6,54.61,53.98,53.5,53.04,52.66,52.39,52.19,52.04,51.9,51.8))
我想要另一列,其中包含针对两个连续值计算的百分比变化。
例如:K= 4 的百分比变化应该用 K =2 计算,类似地,K = 5 用 K= 3 等等。
希望我说清楚了。
有人知道如何解决这个问题吗?
您可以使用 dplyr
中的 lag
。显然,您的前两个值将是 NA
,因为您没有 k = 0 或 k = -1 来评估这些值的百分比变化。
df %>% mutate(new_col = scales::percent(lag(rmse, 2)/rmse))
#> k rmse new_col
#> 1 2 61.41 <NA>
#> 2 3 58.70 <NA>
#> 3 4 56.84 108.040%
#> 4 5 55.60 105.576%
#> 5 6 54.61 104.084%
#> 6 7 53.98 103.001%
#> 7 8 53.50 102.075%
#> 8 9 53.04 101.772%
#> 9 10 52.66 101.595%
#> 10 11 52.39 101.241%
#> 11 12 52.19 100.901%
#> 12 13 52.04 100.673%
#> 13 14 51.90 100.559%
#> 14 15 51.80 100.463%
由 reprex package (v2.0.1)
于 2022-04-18 创建
如果 k4 的 百分比变化 是用 (k4 - k2) / k4 diff
计算的,可以使用滞后 2:
transform(df, change = c(NA, NA, diff(df$rmse, 2)) / df$rmse * 100)
# k rmse change
#1 2 61.41 NA
#2 3 58.70 NA
#3 4 56.84 -8.0401126
#4 5 55.60 -5.5755396
#5 6 54.61 -4.0835012
#6 7 53.98 -3.0011115
#7 8 53.50 -2.0747664
#8 9 53.04 -1.7722474
#9 10 52.66 -1.5951386
#10 11 52.39 -1.2406948
#11 12 52.19 -0.9005557
#12 13 52.04 -0.6725596
#13 14 51.90 -0.5587669
#14 15 51.80 -0.4633205
如果 (k4 - k2) / k2:
transform(df, change = c(NA, NA, diff(df$rmse, 2) / head(df$rmse, -2) * 100))
# k rmse change
#1 2 61.41 NA
#2 3 58.70 NA
#3 4 56.84 -7.4417847
#4 5 55.60 -5.2810903
#5 6 54.61 -3.9232935
#6 7 53.98 -2.9136691
#7 8 53.50 -2.0325948
#8 9 53.04 -1.7413857
#9 10 52.66 -1.5700935
#10 11 52.39 -1.2254902
#11 12 52.19 -0.8925180
#12 13 52.04 -0.6680664
#13 14 51.90 -0.5556620
#14 15 51.80 -0.4611837
我正在尝试查找 2 个连续值的百分比变化。
df = data.frame(k = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),
rmse = c(61.41,58.7,56.84,55.6,54.61,53.98,53.5,53.04,52.66,52.39,52.19,52.04,51.9,51.8))
我想要另一列,其中包含针对两个连续值计算的百分比变化。 例如:K= 4 的百分比变化应该用 K =2 计算,类似地,K = 5 用 K= 3 等等。
希望我说清楚了。 有人知道如何解决这个问题吗?
您可以使用 dplyr
中的 lag
。显然,您的前两个值将是 NA
,因为您没有 k = 0 或 k = -1 来评估这些值的百分比变化。
df %>% mutate(new_col = scales::percent(lag(rmse, 2)/rmse))
#> k rmse new_col
#> 1 2 61.41 <NA>
#> 2 3 58.70 <NA>
#> 3 4 56.84 108.040%
#> 4 5 55.60 105.576%
#> 5 6 54.61 104.084%
#> 6 7 53.98 103.001%
#> 7 8 53.50 102.075%
#> 8 9 53.04 101.772%
#> 9 10 52.66 101.595%
#> 10 11 52.39 101.241%
#> 11 12 52.19 100.901%
#> 12 13 52.04 100.673%
#> 13 14 51.90 100.559%
#> 14 15 51.80 100.463%
由 reprex package (v2.0.1)
于 2022-04-18 创建如果 k4 的 百分比变化 是用 (k4 - k2) / k4 diff
计算的,可以使用滞后 2:
transform(df, change = c(NA, NA, diff(df$rmse, 2)) / df$rmse * 100)
# k rmse change
#1 2 61.41 NA
#2 3 58.70 NA
#3 4 56.84 -8.0401126
#4 5 55.60 -5.5755396
#5 6 54.61 -4.0835012
#6 7 53.98 -3.0011115
#7 8 53.50 -2.0747664
#8 9 53.04 -1.7722474
#9 10 52.66 -1.5951386
#10 11 52.39 -1.2406948
#11 12 52.19 -0.9005557
#12 13 52.04 -0.6725596
#13 14 51.90 -0.5587669
#14 15 51.80 -0.4633205
如果 (k4 - k2) / k2:
transform(df, change = c(NA, NA, diff(df$rmse, 2) / head(df$rmse, -2) * 100))
# k rmse change
#1 2 61.41 NA
#2 3 58.70 NA
#3 4 56.84 -7.4417847
#4 5 55.60 -5.2810903
#5 6 54.61 -3.9232935
#6 7 53.98 -2.9136691
#7 8 53.50 -2.0325948
#8 9 53.04 -1.7413857
#9 10 52.66 -1.5700935
#10 11 52.39 -1.2254902
#11 12 52.19 -0.8925180
#12 13 52.04 -0.6680664
#13 14 51.90 -0.5556620
#14 15 51.80 -0.4611837