I have a dataframe of vertical profile data that has both the downcast and the upcast, however the "downcast" has a lot of ups and downs from trying to identify the thermocline so it's not as simple as indexing the depths and subsetting for differences in depths. I have the maximum depth for each date and site in a separate dataframe, but I can't figure out how to subset the data within each group that comes after the maximum depth.
Date | Site | Depth | Temp |
---|---|---|---|
6/12 | A | 1 | 15 |
6/12 | A | 2 | 14 |
6/12 | A | 3 | 13 |
6/12 | A | 2 | 13 |
6/12 | A | 1 | 14 |
6/12 | B | 1 | 17 |
6/12 | B | 2 | 16 |
6/12 | B | 3 | 13 |
6/12 | B | 4 | 11 |
6/12 | B | 5 | 10 |
6/12 | B | 4 | 10 |
6/12 | B | 3 | 12 |
6/12 | B | 2 | 15 |
6/12 | B | 1 | 17 |
In this example dataframe I'd want to subset after Depth of 3 for 6/12 Site A and after a Depth of 5 for 6/12 Site B. Unfortunately the dataset is very large so I'd like to find code that works as it'll only get larger from here.
I've tried indexing depths and filtering based on differences using:
indexes <- which(diff(Data.2$Depth) > 0)
Upcast <- Data.2[-c(indexes), ]
But because it's not a consistent up and down there's a lot of data left in that I'd like to remove.
which.max(x)
will give you the index where x
achieves its maximum value, and in dplyr row_number()
will give a vector of row numbers for your current group (or the whole data frame if you have not grouped your data).
So, you can group by Date
and Site
, then filter
to rows having a row_number()
greater than which.max(Depth)
library(dplyr)
df %>%
group_by(Date, Site) %>%
filter(row_number() > which.max(Depth))