[R] help wrapping findInterval into a function

R. Michael Weylandt michael.weylandt at gmail.com
Wed Dec 7 16:36:34 CET 2011


Can't this be fixed by switching with to within? E.g.,

x = data.frame(a = 1:3, b = 4:6)

AddRowSums <- function(df) within(df, d <- a + b)

x <- AddRowSums(x)

print(x)

Michael

On Wed, Dec 7, 2011 at 12:07 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Dec 6, 2011, at 5:53 PM, Steve E. wrote:
>
>> Bill (and David),
>>
>> Thank you very much for taking the time to respond to my query.
>>
>> You were right, I was creating and calling the function exactly as you had
>> predicted.  I revised the structure based on your suggestion.  It runs but
>> the output is an array of the flags that are not attached to the data
>> frame,
>> not a new column in the data frame as was my intention.
>>
>> So, the new configuration I tried was like this (where DataFrame is not a
>> real data frame but just the word "DataFrame"):
>>
>> WQFlags <- function(DataFrame) {DataFrame$CalciumFlag <- with(DataFrame,
>> ifelse(variable == "CaD_ICP", (dataqualifier <- c("Y", 'Q', "", "A")
>> [findInterval(DataFrame$value, c(-Inf, 0.027, 0.1, 100, Inf))]),""))
>> }
>>
>> I called it using:
>>
>> WaterQualityData <- WQFlags(WaterQualityData)
>
>
> Unless you provide either the original data or an unambiguous ( at the level
> the R interpreter would see, not at the level of what you see when you print
> a dataframe) description of your data you will get at the very best educated
> guesses. Use str() or dput().
>
>
>>
>> Again, the output is simply an array of the flags, unattached to a data
>> frame.  Can you suggest a way to modify this to make it work as desired,
>> or,
>> in the worst case, can I attach the resulting array of flag values?
>
>
> Do you mean "attach" in the sense of using the R function `attach`? If so,
> then please do not. (And please ignore any advice or the examples concerning
> that issue you might get from reading Crawley's text.)
>
>
> --
>
> Good night.
>
> 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