In the df test
below, I have 5 columns (Site
, Time
, Latitude
, Depth
, and Concentration
). I first interpolated the data to 0.1m intervals, then plot the uninterpolated and interpolated df.
I am trying to get a depth integrated value. So one concentration that is grouped by Site, Time and Latitude. I dont know why I am getting NA’s instead.
I was also reading about library(deSolve) but I couldn’t understand how to manipulate my df into what is provided in the examples.
Is there a way I can get depth integrated by Site, Time and Latitude? The Depth
varies by site so I am unable to just get the sum by Site
or Time
or Latitude
.
dput(test)
structure(list(Site = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Time = c("8/6/2023",
"8/6/2023", "8/6/2023", "8/6/2023", "8/6/2023", "8/6/2023", "8/6/2023",
"8/6/2023", "8/6/2023", "8/6/2023", "8/6/2023", "8/6/2023", "12/6/2023",
"12/6/2023", "12/6/2023", "12/6/2023", "12/6/2023", "12/6/2023",
"12/6/2023", "12/6/2023", "12/6/2023", "12/6/2023", "12/6/2023",
"12/6/2023", "12/6/2023", "12/6/2023", "12/6/2023", "18/6/2023",
"19/6/2023", "20/6/2023", "21/6/2023", "22/6/2023", "23/6/2023",
"24/6/2023", "25/6/2023", "26/6/2023", "27/6/2023", "28/6/2023",
"29/6/2023", "30/6/2023", "9/6/2023", "9/6/2023", "9/6/2023",
"9/6/2023", "9/6/2023", "9/6/2023", "9/6/2023", "9/6/2023", "9/6/2023",
"9/6/2023", "9/6/2023", "15/6/2023", "15/6/2023", "15/6/2023",
"15/6/2023", "15/6/2023", "15/6/2023", "15/6/2023", "15/6/2023",
"15/6/2023", "15/6/2023", "15/6/2023", "15/6/2023", "15/6/2023",
"19/6/2023", "19/6/2023", "19/6/2023", "19/6/2023", "19/6/2023",
"19/6/2023", "19/6/2023", "19/6/2023", "19/6/2023", "19/6/2023",
"24/6/2023", "24/6/2023", "24/6/2023", "24/6/2023", "24/6/2023",
"24/6/2023", "24/6/2023", "24/6/2023", "24/6/2023", "24/6/2023",
"24/6/2023"), Latitude = c(-35.25, -35.25, -35.25, -35.25, -35.25,
-35.25, -35.25, -35.25, -35.25, -35.25, -35.25, -35.25, -35.5,
-35.5, -35.5, -35.5, -35.5, -35.5, -35.5, -35.5, -35.5, -35.5,
-35.5, -35.5, -35.5, -35.5, -35.5, -35.75, -35.75, -35.75, -35.75,
-35.75, -35.75, -35.75, -35.75, -35.75, -35.75, -35.75, -35.75,
-35.75, -35.4, -35.4, -35.4, -35.4, -35.4, -35.4, -35.4, -35.4,
-35.4, -35.4, -35.4, -35.7, -35.7, -35.7, -35.7, -35.7, -35.7,
-35.7, -35.7, -35.7, -35.7, -35.7, -35.7, -35.7, -35.85, -35.85,
-35.85, -35.85, -35.85, -35.85, -35.85, -35.85, -35.85, -35.85,
-35.95, -35.95, -35.95, -35.95, -35.95, -35.95, -35.95, -35.95,
-35.95, -35.95, -35.95), Depth = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L), Concentration = c(NA, NA, NA, 3L,
5L, 7L, 8L, 4L, 3L, 3L, 2L, 1L, NA, 4L, 6L, 7L, 8L, 3L, 5L, 4L,
3L, 2L, 1L, 1L, 1L, 1L, 1L, 5L, 6L, 8L, 9L, 10L, 7L, 5L, 4L,
3L, 2L, 1L, 1L, 1L, NA, NA, NA, NA, 3L, 4L, 6L, 7L, 8L, 9L, 6L,
4L, 6L, 8L, 9L, 7L, 5L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 5L, 6L, 8L,
4L, 3L, 2L, 2L, 2L, 1L, 1L, NA, NA, NA, 6L, 7L, 9L, 5L, 4L, 3L,
2L, 1L)), class = "data.frame", row.names = c(NA, -85L))
do_interp <- function(dat, new_press = seq(0.5, max(dat$Depth, na.rm=TRUE), by = 0.1)) {
out <- tibble(Depth = new_press)
for (var in c('Concentration')) {
out[[var]] <- tryCatch(approx(dat$Depth, dat[[var]], new_press)$y, method = "linear", error = function(e) NA_real_)
}
out
}
test2 <- test %>% group_by(Site, Time, Latitude,) %>% do(do_interp(.))
(plot <- ggplot(data = test, aes(x = Latitude, y = Depth)) +
geom_point(aes(colour = Concentration)) +
scale_y_reverse() +
scale_color_gradientn(colours = rev(rainbow(10))) +
theme_bw() + theme(panel.grid = element_blank()) +
coord_cartesian(clip = "off", expand = 0) +
labs(y = "Depth (m)", x = "Latitude (°S)", colour = "conc"))
(plot2 <- ggplot(data = test2, aes(x = Latitude, y = Depth)) +
geom_point(aes(colour = Concentration)) +
scale_y_reverse() +
scale_color_gradientn(colours = rev(rainbow(10))) +
theme_bw() + theme(panel.grid = element_blank()) +
coord_cartesian(clip = "off", expand = 0) +
labs(y = "Depth (m)", x = "Latitude (°S)", colour = "conc"))
depth_integration <- function(Depth, Concentration) {
depth_diff <- diff(Depth)
area <- sum((Concentration[-1] + Concentration[-length(Concentration)]) * depth_diff) / 2
return(area)
}
depth_integrated_data <- test2 %>%
group_by(Site, Time, Latitude) %>%
dplyr::summarize(depth_integrated_concentration = depth_integration(Depth, Concentration))
print(depth_integrated_data)