[R] help wrapping findInterval into a function
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Dec 8 00:04:04 CET 2011
So I don't know what you are trying to do with your function (and I
might have incidentally messed it up), but within certainly does add
the desired column:
WaterData <- structure(list(Site = structure(c(3L, 3L, 3L, 3L, 3L,
3L), .Label = c("BV",
"CB", "KP", "LA", "MR", "PIE"), class = "factor"), Time = structure(c(1L,
1L, 1L, 1L, 2L, 5L), .Label = c("0:00:00", "0:00:52", "0:01:00",
"0:06:00", "0:07:00", "0:11:00", "0:16:00", "0:21:00", "0:26:00",
"0:36:00", "0:41:00", "0:51:00", "0:56:00", "1:01:00", "1:06:00",
"1:07:00", "1:10:00", "1:11:00", "1:22:00", "1:37:00", "1:52:00",
"10:22:00", "10:37:00", "10:52:00", "11:22:00", "15:55:00", "16:00:00",
"17:25:00", "17:30:00", "18:00:00", "18:05:00", "18:20:00", "18:35:00",
"18:50:00", "19:05:00", "19:20:00", "19:25:00", "19:35:00", "2:07:00",
"2:14:00", "2:16:00", "2:21:00", "2:22:00", "2:26:00", "2:31:00",
"2:36:00", "2:37:00", "2:38:00", "2:44:00", "2:46:00", "2:52:00",
"20:20:00", "20:32:00", "20:35:00", "21:05:00", "21:17:00", "21:20:00",
"21:32:00", "22:05:00", "22:20:00", "22:21:00", "22:32:00", "22:37:00",
"22:42:00", "22:47:00", "22:50:00", "23:16:00", "23:26:00", "23:31:00",
"23:36:00", "23:41:00", "23:46:00", "23:51:00", "23:56:00", "3:07:00",
"3:22:00", "3:52:00", "4:22:00", "4:52:00", "5:22:00", "6:37:00",
"6:57:00", "7:52:00", "9:22:00"), class = "factor"), DateCorrectFmt =
structure(c(4L,
8L, 1L, 3L, 8L, 5L), .Label = c("2010-08-17", "2010-08-21", "2010-08-22",
"2010-08-28", "2010-08-29", "2010-09-21", "2010-09-22", "2010-10-21",
"2011-02-19"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("CaD_ICP", "NaD_ICP", "ZnD_ICP"
), class = "factor"), value = c(0.044, 0.1316, 0.0101, 0.0114,
80.13, 15.42)), .Names = c("Site", "Time", "DateCorrectFmt",
"variable", "value"), row.names = c(1L, 2L, 6L, 8L, 11L, 14L), class =
"data.frame")
WQFunc <- function(df){
within(df, CalcFlag <- ifelse(variable =="CaD_ICP",
(dataqualifier <- c("Y", "Q", "","A")[findInterval(value,c(-Inf,
0.027, 0.1, 100, Inf))]), ""))
}
WQ <- WQFunc(WaterData)
print(WaterData)
print(WQ)
On Wed, Dec 7, 2011 at 5:01 PM, Steve E. <searl at vt.edu> wrote:
> 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.
>
> ______________________________________________
> 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