[R] problem with max in a function
Nikhil Kaza
nikhil.list at gmail.com
Wed Sep 8 04:23:13 CEST 2010
Comments below
On Sep 7, 2010, at 9:37 PM, stephen sefick wrote:
> Here is a striped down example that is not working 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"]),]
If I understand your intent correctly
You can just use (of course be vary of the floating point arithmetic)
measure_bkf_not_zero <- subset(measure_bkf, measure_bkf
$bankfull_depths_m != 0)
>
> 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"])
Are you looking for which. min and which.max?
or just min and max?
>
> bkf_min <- ifelse(length(bkf_min)>1, bkf_min[1], bkf_min)
> bkf_max <- ifelse(length(bkf_max)>1, bkf_max[1], bkf_max)
This is unnecessary if you are using min and max as they just return
one value.
>
> #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
>
> ______________________________________________
> 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