GLEON / rLakeAnalyzer

An R version of Lake Analyzer
43 stars 26 forks source link

Creating a loop for schmidt.stability of different lakes #110

Closed larajan67 closed 3 years ago

larajan67 commented 3 years ago

Hello, I am trying to create a loop to calculate the Schmidt Stability Index via 'schmidt.stability' for a set of lakes, rather than calculating them each manually as there are over 50 lakes. I have created a loop that works for a single lake, but runs into this error with the full dataset: Error in if (max(bthD) > depths[numD]) { : missing value where TRUE/FALSE needed I do have limited experience with loops. I was hoping to see if anyone has done this before and can help?

This is my loop: `for (i in 1:nrow(tpbth)) {

pull out array of temp,depth, bth data by a lake.date

filterx<-filter(tpbth,tpbth$Lake.date==tpbth$Lake.date[i])

create vectors

wtr<-filterx$temp dep<-filterx$depth_t depb<-na.omit(filterx$depth_b) #drop nas bha<-na.omit(filterx$sa_b)

calculate SSI & put into the matrix

m<-rbind(m,schmidt.stability(wtr=wtr,depths = dep,bthA = bha,bthD = depb,sal=0)) }`

jordansread commented 3 years ago

Hi @larajan67

it is hard for us to help address it w/o example data, but if you have a workable loop with one lake, I'd suggest you create a nested loop where the outer loop goes through lakes and the inner loop is your lake-specific loop.

hdugan commented 3 years ago

I agree with @jread-usgs. Also, the missing value where TRUE/FALSE needed is thrown because one of the inputs, either max(bthD) or depths[numD] is not a number. So likely one of your lakes is missing data. By creating nested loops, it's really easy to identify which lake is causing the loop to error-out.

larajan67 commented 3 years ago

Hi @jread-usgs and @hdugan , Thanks for the feedback. Sorry, I forgot to add example data. I have attached some here just as reference test_bthtmp.xlsx

. I am working on the nested loop now and will let you if I figure it out or not.

larajan67 commented 3 years ago

Hi, I did have one NA unaccounted for, which I resolved. I actually got this to work via a vectorize method with dplyr: ssi<-tpbth %>%group_by(Lake.date) %>% summarize(schmidt.stability(wtr=temp,depths = depth_t,bthA = na.omit(sa_b),bthD = na.omit(depth_b),sal =0))

Thanks for your help!