gpt4 book ai didi

r - 计算值(value)反转率/计算具有特定值(value)差异的行数

转载 作者:行者123 更新时间:2023-12-04 15:50:22 25 4
gpt4 key购买 nike

我正在努力计算“值(value)逆转率”,例如特定值范围内的值变化量(=“差距”)。

数据示例:

a <- c(-30,0,20,23,24,25,26,27,28,29,30,5)
Gap <- 10

目标:

计算 10 的值变化量,在这种情况下,解决方案将是 = 4

(1.从-30到0, 2.从0到20, 3.从20,..,到30, 4.从30到5)

任何帮助如何计算这个?

最佳答案

已编辑。现在适用于两个示例:

Gap <- 10

value_changes <- function(vec, gap) {
res <- 0
retain_min <- retain_max <- vec[1]
cat("i:", 1, ", vec[i]:", vec[1], ", retain_min:", retain_min, ", retain_max:", retain_max, ", res:", res, "\n")
for (i in 2:length(vec)) {
if (abs(vec[i] - retain_min) >= gap || abs(vec[i] - retain_max) >= gap) {
res <- res + 1
retain_min <- retain_max <- vec[i]
}
if (vec[i] < retain_min) retain_min <- vec[i]
if (vec[i] > retain_max) retain_max <- vec[i]
cat("i:", i, ", vec[i]:", vec[i], ", retain_min:", retain_min, ", retain_max:", retain_max, "res:", res, "\n")
}
res
}
value_changes(c(5, 25, 17, 7, -2, 12), Gap)
value_changes(c(-30,0,20,23,24,25,26,27,28,29,30,5), Gap)

#i: 1 , vec[i]: 5 , retain_min: 5 , retain_max: 5 , res: 0
#i: 2 , vec[i]: 25 , retain_min: 25 , retain_max: 25 res: 1
#i: 3 , vec[i]: 17 , retain_min: 17 , retain_max: 25 res: 1
#i: 4 , vec[i]: 7 , retain_min: 7 , retain_max: 7 res: 2
#i: 5 , vec[i]: -2 , retain_min: -2 , retain_max: 7 res: 2
#i: 6 , vec[i]: 12 , retain_min: 12 , retain_max: 12 res: 3
#[1] 3
#
#i: 1 , vec[i]: -30 , retain_min: -30 , retain_max: -30 , res: 0
#i: 2 , vec[i]: 0 , retain_min: 0 , retain_max: 0 res: 1
#i: 3 , vec[i]: 20 , retain_min: 20 , retain_max: 20 res: 2
#i: 4 , vec[i]: 23 , retain_min: 20 , retain_max: 23 res: 2
#i: 5 , vec[i]: 24 , retain_min: 20 , retain_max: 24 res: 2
#i: 6 , vec[i]: 25 , retain_min: 20 , retain_max: 25 res: 2
#i: 7 , vec[i]: 26 , retain_min: 20 , retain_max: 26 res: 2
#i: 8 , vec[i]: 27 , retain_min: 20 , retain_max: 27 res: 2
#i: 9 , vec[i]: 28 , retain_min: 20 , retain_max: 28 res: 2
#i: 10 , vec[i]: 29 , retain_min: 20 , retain_max: 29 res: 2
#i: 11 , vec[i]: 30 , retain_min: 30 , retain_max: 30 res: 3
#i: 12 , vec[i]: 5 , retain_min: 5 , retain_max: 5 res: 4
#[1] 4

关于r - 计算值(value)反转率/计算具有特定值(value)差异的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com