r - 根据增加的最小值查找范围
问题描述
我有一个包含多个站点、深度和浓度的数据集。我试图根据最小浓度增加 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 的深度?
我试图使用“哪个”最大值和最小值,但我无法弄清楚代码..如何使用 Dplyr 的 Summarize 和 which() 来查找最小值/最大值
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
解决方案
我不确定我是否完全理解您的要求,但希望这可以帮助您开始。如果它与您的想法不同,请告诉我:
library(dplyr)
df <- data_frame(
station = c(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, 4, 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.6, 0.3, 0.2, 0.2, NA, 0.2, 0.2, 0.2, 0.1, 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 %>%
group_by(station) %>%
mutate(conc_diff = lead(conc) - conc,
dept_diff = lead(depth) - depth) %>%
filter(conc_diff == .1, conc == min(conc, na.rm = TRUE)) %>%
filter(depth == max(depth))
#> # A tibble: 3 x 5
#> # Groups: station [3]
#> station depth conc conc_diff dept_diff
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 13 0.1 0.1 1
#> 2 2 11 0.1 0.1 3
#> 3 3 10 0.1 0.1 5
由reprex 包(v0.3.0)于 2020-06-17 创建
推荐阅读
- r - R删除Wordcloud中单词之间的距离
- java - 更改从 jar 文件中获取的 jtable 中特定行的颜色
- php - .htaccess RewriteRule 键作为文件夹,然后是值
- ios - 基于手机调整UIScrollView内UIView和UIButton之间的约束高度
- reactjs - 如何在谷歌云应用引擎上进行 url 重写?
- vba - 动态检查与 Worksheets -VBA Excel 同名的复选框
- c# - 尝试在 Dot Net Core 2.0 中解码 JWT
- mongodb - mongodb中的unpivot分组列
- c# - 读取 Helpers 的 URL 参数
- php - 我如何在 php 中优化我的代码以进行循环