[R] help wrapping findInterval into a function

Steve E. searl at vt.edu
Wed Dec 7 23:01:37 CET 2011


Thanks to everyone for continued assistance with this problem.  I realize
that I had not included enough information, hopefully I have done so here. 
I attached a dput output of a sample of the data titled 'WaterData' (and str
output below).  Below are dput outputs of the function I am trying to get
working and the resulting array when I run it.  Unfortunately, Michael,
changing 'with' to 'within' did not solve the problem, as running the
function in that case produced no discernible output or result.  What I
meant by the function now producing an array of values (though the result I
am looking for) that are not attached to the data frame, is that they show
up separately in a result window (in a similar format to what you get from
dput() and are not at all associated with the data frame).  Again, thanks so
much!

> dput(WQFunc)
function (dataframe) 
{
    dataframe$CalcFlag <- with(dataframe, ifelse(variable == 
        "CaD_ICP", (dataqualifier <- c("Y", "Q", "",
"A")[findInterval(dataframe$value, 
        c(-Inf, 0.027, 0.1, 100, Inf))]), ""))
}

> str(WaterData)
'data.frame':	126 obs. of  5 variables:
 $ Site          : Factor w/ 6 levels "BV","CB","KP",..: 3 3 3 3 3 3 3 3 3 3
...
 $ Time          : Factor w/ 84 levels "0:00:00","0:00:52",..: 1 1 1 1 2 5
16 16 19 20 ...
 $ DateCorrectFmt: Factor w/ 9 levels "2010-08-17","2010-08-21",..: 4 8 1 3
8 5 5 8 8 8 ...
 $ variable      : Factor w/ 3 levels "CaD_ICP","NaD_ICP",..: 1 1 1 1 1 1 1
1 1 1 ...
 $ value         : num  0.044 0.1316 0.0101 0.0114 80.13 ...

Below is the output I get if if I run the WQFunc as:
flagged <- WQFunc(WaterData)

> dput(Flagged)
c("Q", "", "Y", "Y", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
)
> 
 

Again, though, 'Flagged' is an array of those values in a output window but
are not 'attached' to WaterData.

--
View this message in context: http://r.789695.n4.nabble.com/help-wrapping-findInterval-into-a-function-tp4165464p4170688.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list