Im not sure how to perform this type of subset. In the code below, I have 4 variables depth
in meters, group
, value
and cutoff
in meters. I interpolated the variables from death of 5 to the max depth.
What I need to do next that I dont know how is to subset the data based on the cutoff
. For example, if cutoff
is 15m and 16m respectively for each of the group
, how can I subset the data so that the interpolated data (i.e., group, depth, value and cutoff) all only go to the cut off value of 15m for group 1 and 16m for group 2 and the rest of the data is omitted? Essentially the subset is based on what the cutoff number is.
group <- c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2)
depth <- c(5, 10, 20, 25, 5, 7, 15, 25, 30, 35)
value <- c(3.2, 6.4, 12.3, 15.2, 2, 5.3, 10.2, 21.2, 24.8, 29.3)
cutoff <- c(15, 15, 15, 15, 16, 16, 16, 16, 16, 16)
df <- as.data.frame(cbind(group, depth, value, cutoff))
interp <- function(dat, new_depth = seq(5, max(dat$depth), by = 1)) {
out <- tibble(depth = new_depth)
for (var in c('value', 'cutoff')) {
out[[var]] <- tryCatch(approx(dat$depth, dat[[var]], new_depth)$y, method = "linear", error = function(e) NA_real_)
}
out
}
df_2 <-df %>% group_by(group) %>% do(interp(.))