Return value at deepest point in dataset

Hello contributors!

I have a water quality dataset where I am trying to have R return the value of DO at the deepest point in the water column. For example, in the below dataset, at depth 11, the DO is 2.00. I want R to do that automatically and to set that value to BOTTOM_DO.

df <- structure(list(X = 1:13, SITE_ID = c("GBA20-10501", "GBA20-10501", 
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", 
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", 
"GBA20-10501"), DEPTH = c(0.1, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11), LINE = c(13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 
13.5, 13.5, 13.5, 13.5, 13.5), TEMPERATURE = c(21.95, 21.9, 21.9, 
21.85, 21.7, 21.65, 21.55, 21.3, 20.35, 18.3, 16.8, 15.95, 15.7
), DO = c(9.6, 9.6, 9.7, 9.55, 9.45, 9.3, 8.95, 8.6, 6.55, 4.85, 
2.35, 2.05, 2), PH = c(8.64, 8.64, 8.645, 8.64, 8.62, 8.6, 8.565, 
8.485, 8.255, 8.06, 7.955, 7.94, 7.935), CONDUCTIVITY = c(309.75, 
309.75, 309.75, 309.6, 310.05, 310.65, 310.35, 310.6, 309, 299.95, 
292.55, 287.55, 287.8), LIGHT_AMB = c(204.15, 204.35, 204.2, 
204.15, 204.25, 204.3, 204.25, 204.15, 204.4, 204.1, 204.15, 
204.2, 204.15), LIGHT_UW = c(985.4, 853.1, 834.5, 332.65, 124.7, 
64.55, 31.1, 15.05, 7.65, 3.8, 2.25, 0.9, 0.25)), row.names = c(NA, 
13L), class = "data.frame", na.action = structure(c(`14` = 14L), class = "omit"))
>

I can figure out the deepest point using:

max(df$DEPTH, na.rm=T)

But I'm not having much luck figuring out how to use this value to obtain the DO at the point.

Thanks so much for your help!

Hi,

Use which() to return the index. In your case I guess

df$DO[which.max(df$DEPTH)]
[1] 2

JW

Thank yooooou - I know about the which() function but I couldn't figure out how to apply it. Much appreciated!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.