You need to enable JavaScript to run this app. R语言基于列名匹配查找数值计算监测井相对水头的方法
优惠活动
大模型
产品
解决方案
定价
更多

R语言基于列名匹配查找数值计算监测井相对水头的方法

R语言实现方案

方法1:dplyr(tidyverse生态)实现

该方案完全通过列名匹配完成计算,无硬编码的数值索引:

library(dplyr)

relative_head <- hydraulic.head.mw %>%
  mutate(
    across(
      # 仅匹配ground.levels中存在的监测井列名,自动跳过date列
      .cols = all_of(names(ground.levels)),
      # 按当前列名匹配取对应地面标高做减法
      .fns = ~ .x - ground.levels[[cur_column()]][[1]]
    )
  )

方法2:Base R实现

无需加载第三方包,同样仅通过列名匹配操作:

# 提取两个数据框共有的监测井列名,自动排除date列
target_cols <- intersect(names(ground.levels), names(hydraulic.head.mw))

relative_head <- hydraulic.head.mw
# 逐列计算相对水头
relative_head[target_cols] <- Map(
  function(col_vals, col_name) col_vals - ground.levels[[col_name]][1],
  relative_head[target_cols],
  target_cols
)

说明

  • 两种方法均自动按列名匹配对应关系,intersect/all_of逻辑会自动忽略仅在单个数据框中存在的列,避免列名不匹配报错
  • 计算逻辑严格遵循要求:取ground.levels对应列的第一个值作为减数,对hydraulic.head.mw对应列全量做减法

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 23:24:02