【发布时间】:2020-10-07 05:29:09
【问题描述】:
我有一个包含多个站点、深度和浓度的数据集。我试图根据最小浓度增加 0.1 的位置找到深度(或厚度)的差异
例如:在1号站,最大深度为14m。在 4m 处有 0.1 的浓度,在 6m 处增加到 0.2。但随后它在 10m 处再次下降到 0.1,并一直保持这种状态,直到 12m 才增加。它在 13m 处仅增加 0.05。在14m处,浓度增加0.1。所以 14m 是找到最低浓度的最深(或最大深度)。我需要找到一种方法来修复我的代码以找到 14...(即浓度增加 0.1)。我可以找到给定站点的最大深度和最小浓度。
此代码为我提供了每个站点的最大深度 (max_depth) 列和每个站点的最小浓度 (min_conc) 的另一列。
我如何找到最低浓度增加 0.1 的深度?
我正在尝试使用“哪个”最大值和最小值,但我无法弄清楚代码..How to use Dplyr's Summarize and which() to lookup min/max values
station <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4)
depth <- c(1, 2, 3, 6, 8, 9, 10, 11, 12, 13, 14, 1, 3, 4, 6, 8, 10, 11, 14, 1, 2, 4, 6, 8, 9, 10, 15, 18, 20, 1, 2, 4, 6, 8, 10, 11)
conc <- c(0.4, 0.4, 0.3, 0.1, 0.2, NA, 0.2, 0.1, 0.1, 0.1, 0.15, 0.2, 0.5, 0.4, 0.3, 0.6, 0.4, 0.2, 0.1, 0.2, 0.3, 0.2, 0.5, 0.5, 0.3, 0.2, 0.1, 0.2, 0.2, 0.2, 0.8, 0.6, 0.4, 0.3, 0.2, 0.3, 0.3)
df <- cbind(station, depth, conc)
(df <- as.data.frame(df))
(depth <- df %>%
group_by(station) %>%
summarize(
Max_depth=miss(max(depth)),
min_conc=miss(min(conc, na.rm=TRUE)),
press_depth = depth[tail(which(conc == min(conc, na.rm = TRUE)), 1)]))
当我尝试这样做时:
press_depth = depth[tail(which(conc == min(conc > 0.1, na.rm = TRUE)), 1)])
我收到一个错误:列 press_depth 的长度必须为 1(汇总值),而不是 0
【问题讨论】:
标签: r function dplyr summarize