[R] problem with max in a function

David Winsemius dwinsemius at comcast.net
Wed Sep 8 04:24:47 CEST 2010


On Sep 7, 2010, at 10:05 PM, stephen sefick wrote:

> I'm sorry.  In the
>
> bkf_max <- grep(max(measure_bkf_not_zero[,"bankfull_depths_m"]),
> measure_bkf_not_zero[,"bankfull_depths_m"])
>
> it is giving the indexes for 1 5 10 15 16

And it was supposed to yield ... what?
>
> I think this is because grep encounters a 1 with either 0.

Huh?

> or nothing
> in front of it.  I would like to find the max and then then the
> closest (leftmost) which is why the ifelse statment follows.

grep is for working with character vectors. It seems to work when  
given numerics as patterns, but with floating point representations it  
seems really dangerous. I'm rather amazed you got anything vaguely  
useful. (And see further negative comments on that strategy below.)   
If you want the max of a numeric vector, then use max or which.max if  
you want the index. If you want the next largest then use max( vector[- 
which.max(vector)] ). So as Jim Holtman's tag line says: what problem  
are you trying to solve?


>
> Again, I am sorry for being vague.  I get wrapped up in a problem and
> forget that I need to communicate.
>
> kindest regards,
>
> Stephen
>
> On Tue, Sep 7, 2010 at 8:48 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>>
>> On Sep 7, 2010, at 9:37 PM, stephen sefick wrote:
>>
>>> Here is a striped down example that is not working
>>
>> That dreadful phrase... "is not working". When the ESP package  
>> comes to
>> fruition, life will be so easy. Until then ... the English language  
>> is
>> necessary. Where am we supposed to be looking. Did I miss you  
>> saying which
>> of those (unprinted) objects we should be fixing.
>>
>>> because of the 1.00
>>> to 1.  Any help would be greatly appreciated.
>>>
>>> measure_bkf <- (structure(list(measurment_num = c(0, 0.2, 0.4, 0.6,
>>> 0.8, 1, 1.2,
>>> 1.4, 1.6, 1.8, 2, 2.2, 2.2, 2.4, 2.6, 2.8), bankfull_depths_m =  
>>> c(-0.15,
>>> -0.09, -0.00999999999999998, 0.06, 0.13, 0.26, 0.36, 0.46, 0.56,
>>> 0.61, 0.85, 0.93, 0.93, 0.97, 1, 1)), .Names = c("measurment_num",
>>> "bankfull_depths_m"), row.names = c(32L, 1L, 2L, 3L, 4L, 5L,
>>> 6L, 7L, 8L, 9L, 10L, 11L, 29L, 12L, 13L, 14L), class =  
>>> "data.frame"))
>>>
>>>
>>>
>>> measure_bkf_not_zero <- measure_bkf[grep("[^0]",
>>> measure_bkf[,"bankfull_depths_m"]),]

You have constructed an odd pattern with the square brackets around  
caret-zero. Per the regex page: "A character class is a list of  
characters enclosed between [ and ] which matches any single character  
in that list; unless the first character of the list is the caret ^,  
when it matches any character not in the list" You are trying to find  
any number with a non-zero in a numeric vector? That seems to be what  
happened.


>>>
>>> bkf_min <- grep(min(measure_bkf_not_zero[,"bankfull_depths_m"]),
>>> measure_bkf_not_zero[,"bankfull_depths_m"])
>>>
>>> bkf_max <- grep(max(measure_bkf_not_zero[,"bankfull_depths_m"]),
>>> measure_bkf_not_zero[,"bankfull_depths_m"])
>>>
>>> bkf_min <- ifelse(length(bkf_min)>1, bkf_min[1], bkf_min)
>>> bkf_max <- ifelse(length(bkf_max)>1, bkf_max[1], bkf_max)
>>>
>>> #s <- with(measure_bkf_not_zero, approx(measurment_num,
>>> bankfull_depths_m,
>>> xout=seq(measure_bkf_not_zero[bkf_min,"measurment_num"],
>>> measure_bkf_not_zero[bkf_max,"measurment_num"], length=2000)))
>>> #int_bkf <- with(s, x[which.min(y[y>0])])
>>>
>>> s <- with(measure_bkf_not_zero[bkf_min:bkf_max,],
>>> approxfun(bankfull_depths_m, measurment_num), ties=mean)
>>>
>>> int_bkf <- s(0)
>>>
>>>
>>>
>>> On Tue, Sep 7, 2010 at 8:28 PM, David Winsemius <dwinsemius at comcast.net 
>>> >
>>> wrote:
>>>>
>>>> On Sep 7, 2010, at 9:06 PM, stephen sefick wrote:
>>>>
>>>>> s <- 1.00
>>>>> max(s)
>>>>
>>>>> sprintf("%.2f", max(s))
>>>>
>>>> [1] "1.00"     @ as a string/character object
>>>>>
>>>>> returns 1
>>>>>
>>>>> is there anyway that I can get it to return 1.00.  I am using the
>>>>> results of this max statement in a grep statement and it returns  
>>>>> the
>>>>> wrong numbers,  I will provide more information and code if it  
>>>>> would
>>>>> make more sense in context.
>>>>>
>>>>> -- Stephen Sefick
>>>>> ____________________________________
>>>>> | Auburn University                                   |
>>>>> | Department of Biological Sciences           |
>>>>> | 331 Funchess Hall                                  |
>>>>> | Auburn, Alabama                                   |
>>>>> | 36849                                                    |
>>>>> |___________________________________|
>>>>> | sas0025 at auburn.edu                             |
>>>>> | http://www.auburn.edu/~sas0025             |
>>>>> |___________________________________|
>>>>>
>>>>> Let's not spend our time and resources thinking about things  
>>>>> that are
>>>>> so little or so large that all they really do for us is puff us  
>>>>> up and
>>>>> make us feel like gods.  We are mammals, and have not exhausted  
>>>>> the
>>>>> annoying little problems of being mammals.
>>>>>
>>>>>                               -K. Mullis
>>>>>
>>>>> "A big computer, a complex algorithm and a long time does not  
>>>>> equal
>>>>> science."
>>>>>
>>>>>                             -Robert Gentleman
>>>>> ______________________________________________
>>>>> 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
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Stephen Sefick
>>> ____________________________________
>>> | Auburn University                                   |
>>> | Department of Biological Sciences           |
>>> | 331 Funchess Hall                                  |
>>> | Auburn, Alabama                                   |
>>> | 36849                                                    |
>>> |___________________________________|
>>> | sas0025 at auburn.edu                             |
>>> | http://www.auburn.edu/~sas0025             |
>>> |___________________________________|
>>>
>>> Let's not spend our time and resources thinking about things that  
>>> are
>>> so little or so large that all they really do for us is puff us up  
>>> and
>>> make us feel like gods.  We are mammals, and have not exhausted the
>>> annoying little problems of being mammals.
>>>
>>>                                -K. Mullis
>>>
>>> "A big computer, a complex algorithm and a long time does not equal
>>> science."
>>>
>>>                              -Robert Gentleman
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>
>
>
> -- 
> Stephen Sefick
> ____________________________________
> | Auburn University                                   |
> | Department of Biological Sciences           |
> | 331 Funchess Hall                                  |
> | Auburn, Alabama                                   |
> | 36849                                                    |
> |___________________________________|
> | sas0025 at auburn.edu                             |
> | http://www.auburn.edu/~sas0025             |
> |___________________________________|
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
>                                 -K. Mullis
>
> "A big computer, a complex algorithm and a long time does not equal  
> science."
>
>                               -Robert Gentleman



More information about the R-help mailing list