You need to enable JavaScript to run this app. R语言编写循环调整相邻点坡度至满足最大阈值条件的脚本
优惠活动
大模型
产品
解决方案
定价
更多

R语言编写循环调整相邻点坡度至满足最大阈值条件的脚本

R语言坡度合规高程自动调整需求

我希望编写一段R语言脚本,重复执行条件测试或调整函数,直到所有坡度值均满足指定要求;此前我可通过复制粘贴代码实现该逻辑,但该方式操作繁琐易出错。

更新说明:我此前曾用模拟数据和模拟条件提问,但始终未能解决问题,以下为实际业务场景需求:我需要调整同一序列内多个相邻点之间的坡度,生成满足坡度小于0.02要求的平滑高程序列。

初始参数与业务数据

# 初始参数定义:
NS_max_slope <- 0.02
# NS即南北向坡度:北向坡度为正值,南向坡度为负值,本次仅校验北向坡度的合规性

# 业务数据结构
        Z   LF_IN_1 old_z_1 NS_slope    include
1   285.2573    NA  NA  NA  NA
2   285.2283    16.4147 285.2573    -0.001766709    -0.001766709
3   285.154 24.3543 285.2283    -0.003050796    -0.003050796
4   285.0791    24.3543 285.154 -0.003075432    -0.003075432
5   283 24.3544 285.0791    -0.085368558    -0.085368558
6   284.8716    19.0761 283 0.098112298 0.098112298
7   284.8364    19.0761 284.8716    -0.001845241    -0.001845241
8   284.788 24.3543 284.8364    -0.001987329    -0.001987329
9   284.7599    24.3544 284.788 -0.001153796    -0.001153796
10  284.9187    24.3543 284.7599    0.006520409 0.006520409
11  285 16.4147 284.9187    0.004952878 0.004952878
12  280 215.3846194 285 -0.023214285    NA

坡度校验功能(slope_check())

判断相邻两点间的坡度是否超限,超限规则为*坡度值大于NS_max_slope(0.02)*即为不达标:

fail_slope <- c()
fail_slope_na <- c()
fail_slope_no <- c()
fail_slope_yes <- c()

slope_check <- function(slope){
    for (s in slope) {
        if (is.na(s)) {
            fail_slope_na <- NA
            fail_slope <- c(fail_slope, fail_slope_na)
        } else if (s <= NS_max_slope) {
            fail_slope_no <- "NO"
            fail_slope <- c(fail_slope, fail_slope_no)
        } else {
            fail_slope_yes <- "YES"
            fail_slope <- c(fail_slope, fail_slope_yes)
        }
    }
    return(fail_slope)
}

高程调整功能(elev_adj())

对经坡度校验判定为超限的点位高程进行调整:

new_slope <- c()
new_slope_na <- c()
new_slope_no <- c()
new_slope_yes <- c()
new_slope_counter <- 1

elev_adj <- function(data) {
    for (sf in data$slope_fail) {
        if (is.na(sf)) {
            new_slope_na <- data$Z[new_slope_counter]
            new_slope <- c(new_slope, new_slope_na)
            new_slope_counter <- new_slope_counter + 1
        } else if (sf == "NO") {
            new_slope_no <- data$Z[new_slope_counter]
            new_slope <- c(new_slope, new_slope_no)
            new_slope_counter <- new_slope_counter + 1
        } else {
            new_slope_yes <- lag_z[new_slope_counter] + data$LF_IN_1[new_slope_counter] * NS_max_slope
            new_slope <- c(new_slope, new_slope_yes)
            new_slope_counter <- new_slope_counter + 1
        }
    }
    return(new_slope)
}

迭代执行功能(elev_adjust())

循环调用slope_check()和elev_adj()函数,直到所有点位的坡度都满足合规要求。
示例:第6行坡度值为0.098,远大于0.02的最大坡度阈值,需将对应高程传入elev_adj()函数调整(首次调整后该点位高程为280.3283);该高程调整完成后,第7行的坡度会触发slope_check()判定为超限,也需传入elev_adj()函数调整,如此持续迭代直到所有坡度均满足条件。
当前初步实现的迭代函数逻辑如下:

elev_adjust <- function(data) {
    
    ## 坡度超限校验
    slope_check_df <- data %>% 
        mutate(slope_fail = slope_check(slope = include))
    
    # 坡度调整
    iteration_1 <- slope_check_df %>% mutate(elev_adjust_1 = elev_adj(data = slope_check_df),
                                             elev_adjust_lag_1 = lag(elev_adjust_1),
                                             slope_adj_1 = (elev_adjust_1 - elev_adjust_lag_1) / LF_IN_1, 
                                             slope_check_1 = slope_check(slope_adj_1))
    
    iteration_1
}

当前实现难点

所有调整逻辑依赖前一行的计算结果,无法通过直接计算完成全流程迭代,需要相关实现思路与代码指导。


内容的提问来源于stack exchange,提问作者strockerik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.28 03:06:02