0

I have a dataset with multiple stations, depths and concentration. I am trying to find the difference in depth (or the thickness) based on where the minimum concentration increases by 0.1

For example: At station 1, the maximum depth is 14m. There is a conc of 0.1 at 4m and it increases to 0.2 at 6m. But then it goes down again to 0.1 at 10m and stays that way till 12m before it increases. It increases only by 0.05 at 13m. At 14m, the concentration is increased by 0.1. So 14m is the deepest (or maximum depth) where the lowest conc is found. I need to find a way to fix my code to find that 14... (i.e. where concentration increases by 0.1). I can find the max depth for a given station and the minimum concentration.

This code gives me a column with maximum depth for each station (max_depth) and another column on what the minimum concentration is for each station (min_conc).

How do I find at what depth does the lowest concentration increase by 0.1?

Im trying to use 'which' max and min but I can't figure out the code.. 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)]))

when I try this instead:

 press_depth = depth[tail(which(conc == min(conc > 0.1, na.rm = TRUE)), 1)])

I get an error: Column press_depth must be length 1 (a summary value), not 0

L55
  • 117
  • 8

1 Answers1

0

I'm not sure I understood completely what you are asking, but hopefully this can help you start. If it's different from what you have in mind, let me know:

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

Created on 2020-06-17 by the reprex package (v0.3.0)

GGamba
  • 13,140
  • 3
  • 38
  • 47
  • what does the lead do? Say if 0.2 is the lowest concentration, there can be multiple depths where 0.2 occurs. I need to find where the 0.2 is at its deepest. Im trying to follow an article where it says "defined here as the distance between the conc +0.1 depth and the profile bottom depth" – L55 Jun 17 '20 at 13:03
  • `lead(x)` 'looks ahead' in the column. `conc_diff = lead(conc) - conc` is the difference in `conc` at each depth. It's hard to understand without a complete example. I'm updating the answer reflecting what I think I understood – GGamba Jun 17 '20 at 13:09
  • Somehow it doesn't work on my code. I get an error Factor `site` contains implicit NA, consider using `forcats::fct_explicit_na`. I tried updating my code above to reflect the problem better (I hope). At station 1, there are 11 depths that range from 1m - 14m. The lowest concentration is 0.1 and it occurs at depth of 6m, 11, 12, and 13m before it goes up by 0.1 at 14m. I need to find the max depth where the lowest concentration occurs. In this case its 13m. – L55 Jun 17 '20 at 13:27
  • The `site` column is not in the data you shared, sry can't help you there. Updating the answer, with you latest data, showing it works as you requested – GGamba Jun 17 '20 at 13:46
  • Updated the main question above. – L55 Jun 17 '20 at 13:48