I have a list of data frames, and I'd like to apply a function to that list to find the location in the "julian" column that corresponds to half the max value in the "total_cover" column. Here's some data that represents the data I have:
df1 <- data.frame(julian = c(81,85,88,97,101,104,126,167),
total_cover = c(43,52,75,92,94,97,188,172))
df2 <- data.frame(julian = c(81,85,88,97,101,104,126,167),
total_cover = c(30,55,73,80,75,85,138,154))
df3 <- data.frame(julian = c(107,111,115,119,123,129,131,133,135,137),
total_cover = c(36,41,43,47,55,55,55,65,75,80))
data.list <- list(df1=df1,df2=df2,df3=df3)
The code below is what I've tried, but I'm not getting the correct output. This doesn't seem to be finding the julian day that corresponds to half the max value
unlist(lapply(X = data.list, FUN = function(x){
x[which.max(x[["total_cover"]] >= which.max(x[["total_cover"]])/2), "julian"]
}))
output:
df1 df2 df3
81 81 107
My ideal output would be what's shown below, with the julian dates that correspond to >= max(total_cover)/2
df1 df2 df3
101 97 111
Using R 4.2.2
I believe the following answers the question.
sapply(data.list, \(x) {
half_max <- max(x$total_cover)/2
d <- abs(x$total_cover - half_max)
is.na(d) <- x$total_cover < half_max
x$julian[which.min(d)]
})
#> df1 df2 df3
#> 101 97 111
Created on 2022-12-13 with reprex v2.0.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With