Dear R Community, I hope you might be able to assist with a small problem creating a function. I am working with water-quality data sets that contain the concentration of many different elements in water samples. I need to assign quality-control flags to values that fall into various concentration ranges. Rather than a web of nested if statements, I am employing the findInterval function to identify values that need to be flagged and to assign the appropriate flag. The data consist of a sample identifier, the analysis, and corresponding value. The findInterval function works well; however, I would like to incorporate it into a function so that I can run multiple findInterval functions for many different water-quality analyses (and I have to do this for many dataset) but it seems to fall apart when incorporated into a function.
Run straighforward, the findInterval function works as desired, e.g. below, creating the new CalciumFlag column with the appropriate flag for, in this case, levels of calcium in the water: WQdata$CalciumFlag <- with(WQdata, ifelse(analysis == "Calcium", (flags <- c("Y", 'Q', "", "A") [findInterval(WQdata$value, c(-Inf, 0.027, 0.1, 100, Inf))]),"")) However, it does not worked when wrapped in a function (no error messages are thrown, it simply does not seem to do anything): WQfunc <- function() { WQdata$CalciumFlag <- with(WQdata, ifelse(analysis == "Calcium", (flags <- c("Y", 'Q', "", "A") [findInterval(WQdata$value, c(-Inf, 0.027, 0.1, 100, Inf))]),"")) } Calling the function WQfunc() does not produce an error but also does not produce the expected CalciumFlag, it seems to not do anything. Ultimately, what I need to get to is something like below where multiple findInterval functions for different analyses are included in a single function, then I can concatenate the results into a single column containing all flags for all analyses, e.g.: WQfunc <- function() { WQdata$CalciumFlag <- with(WQdata, ifelse(analysis == "Calcium", (flags <- c("Y", 'Q', "", "A") [findInterval(WQdata$value, c(-Inf, 0.027, 0.1, 100, Inf))]),"")) WQdata$SodiumFlag <- with(WQdata, ifelse(analysis == "Sodium", (flags <- c("Y", 'Q', "", "A") [findInterval(WQdata$value, c(-Inf, 0.050, 0.125, 125, Inf))]),"")) WQdata$MagnesiumFlag <- with(WQdata, ifelse(analysis == "Magnesium", (flags <- c("Y", 'Q', "", "A") [findInterval(WQdata$value, c(-Inf, 0.065, 0.15, 75, Inf))]),"")) .....etc for additional water-quality analyses... } As an aside, I started working with the findInterval tool from an example that I found online but am not clear as to how the multi-component configuration incorporating brackets actually works, can anyone suggest a good resource that explains this? I thank you very much for any assistance you may be able to provide. Regards, Steve -- View this message in context: http://r.789695.n4.nabble.com/help-wrapping-findInterval-into-a-function-tp4165464p4165464.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.