[R] help wrapping findInterval into a function
William Dunlap
wdunlap at tibco.com
Tue Dec 6 21:46:26 CET 2011
No, the problem is not with "with" but is
that the OP's did not return the modified
data.frame. He didn't show how the function
was called, but I suspect the usage was like
f0 <- function() globalDataFrame$newCol <- ...
f0()
where it should have been
f1 <- function(dataFrame) {
dataFrame$newCol <- ...
dataFrame # return modified dataFrame
}
globalDataFrame <- f1(globalDataFrame)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
> Sent: Tuesday, December 06, 2011 12:05 PM
> To: Steve E.
> Cc: r-help at r-project.org
> Subject: Re: [R] help wrapping findInterval into a function
>
>
> On Dec 6, 2011, at 11:43 AM, Steve E. wrote:
>
> > 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.
>
> It's probably your use of "with" rather than findInterval. The 'with'
> function sets up an environment and is used mostly in interactive
> session. If you have not passed a dataframe into the function then you
> should use the name of the dataframe and '['.
>
>
> > 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 at 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list