# [R] Additional field data collection

David Winsemius dwinsemius at comcast.net
Wed Feb 3 23:14:36 CET 2010

```On Feb 3, 2010, at 3:36 PM, stephen sefick wrote:

> This is a subset of a much larger dataframe.  I would like to be able
> to automate finding the pair of x, y coordinates where the line
> crosses zero agian
>
> x <- (structure(list(bankfull_depths_m = c(0, 0.17, 0.38, 0.37, 0.36,
> 0.39, 0.47, 0.48, 0.19, 0.05, -0.05, -0.09), measurment_num_m = c(0.2,
> 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4)), .Names =
> c("bankfull_depths_m",
> "measurment_num_m"), class = "data.frame", row.names = 97:108))
>
> qplot(measurment_num_m, bankfull_depths_m, data=x)
>
> in this case it is 2.1, 0

I cannot quite get there with a naive application of approxfun using
"reversed arguments", since the inverse function that would be created
is not a legitimate function. I can do it in segments, though:

> y.x <- approxfun(x=x\$bankfull_depths_m[9:12], y=x
\$measurment_num_m[9:12])
> y.x(0)
[1] 2.1  # so you can do it piecewise in regions where the x-y
function is monotonic

> y.x <- approxfun(x=x\$bankfull_depths_m, y=x\$measurment_num_m)
> y.x(0)
[1] 0.2  # an almost trivial result at the LHS of the range.

You could also try to do a Newtonian walk (at least that is my guess
for the underlayment of uniroot() )  along the result of the the
unreversed arguments.

> x.y <- approxfun(y=x\$bankfull_depths_m, x=x\$measurment_num_m)
> uniroot(x.y, range(x\$measurment_num_m) )
\$root
[1] 0.2

\$f.root
[1] 0

\$iter
[1] 0

\$estim.prec
[1] 0

# That was the first root and this is the second.

> uniroot(x.y, c(1, 2.3) )
\$root
[1] 2.1

\$f.root
[1] 0

\$iter
[1] 3

\$estim.prec
[1] 0.01162791

So you still need to apply some guidance to the functions.

--
david

>
> I need a way to process a whole bunch of data points, and I am at a
> loss.  Thanks for any help.
> regards,

Whole bunch??? Can you be any more vague, please?
--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

```