Dear List, I have the following code: x <-c(0, 13.8333333333333, 38.1666666666667, 62.1666666666667, 85.9166666666667, 108.916666666667) y <-c(1.77, 2.39, 3, 2.65, 2.62, 1.8) Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") plot(Interpolated) in this code x is time in hours (cumulative), and y is a biological variable. I am using linear interpolation assuming getting from y(i) to y(i+1) is a linear path. I would like to calculate the time in this case that it takes to surpass the y value of 2 for the first time. If you look at the plot, you will see that the y value of 2 is crossed on 2 occasions: between the first and second value of y (ie:1.77 to 2.39) and the fifth and sixth value of y (ie2.62 and 1.8). In my case I really only care about the first "section" (ie: how long it takes to get from 1.77 to 2.0). Any thoughts on how I could calculate that chunk of the time would be greatly appreciated. let me present it in a simpler idea wher I can actually tell you what the answer should be without having the code: x <-c(0, 12, 24, 36, 48, 60) y <-c(1.5, 2, 3, 2.65, 2.62, 1.8) Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") plot(Interpolated) the answer in this case is 12 hours, which is the time spent in getting from 1.5 to 2... hope I explained clearly what I am trying to do appreciate the help, Andras [[alternative HTML version deleted]]
Hi Andras, This may be too simplistic, but how about with(Interpolated, x[y==2][1]) ? On Tue, Feb 26, 2013 at 4:21 PM, Andras Farkas <motyocska at yahoo.com> wrote:> Dear List, > > I have the following code: > > x <-c(0, 13.8333333333333, 38.1666666666667, 62.1666666666667, 85.9166666666667, 108.916666666667) > y <-c(1.77, 2.39, 3, 2.65, 2.62, 1.8) > Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") > plot(Interpolated) > > in this code x is time in hours (cumulative), and y is a biological variable. I am using linear interpolation assuming getting from y(i) to y(i+1) is a linear path. I would like to calculate the time in this case that it takes to surpass the y value of 2 for the first time. If you look at the plot, you will see that the y value of 2 is crossed on 2 occasions: between the first and second value of y (ie:1.77 to 2.39) and the fifth and sixth value of y (ie2.62 and 1.8). In my case I really only care about the first "section" (ie: how long it takes to get from 1.77 to 2.0). Any thoughts on how I could calculate that chunk of the time would be greatly appreciated. > > let me present it in a simpler idea wher I can actually tell you what the answer should be without having the code: > > x <-c(0, 12, 24, 36, 48, 60) > y <-c(1.5, 2, 3, 2.65, 2.62, 1.8) > Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") > plot(Interpolated) > > the answer in this case is 12 hours, which is the time spent in getting from 1.5 to 2... > > hope I explained clearly what I am trying to do > > appreciate the help, > > Andras > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. >
f <- function(x, y, criticalY) { # return x value of first upcrossing of y past criticalY stopifnot(length(x) == length(y), length(x)>1, length(criticalY)==1) i <- seq_along(x)[-1] w <- which(y[i] >= criticalY & y[i-1] <= criticalY)[1] # pos of first upcrossing if (is.na(w)) { # no upcrossing NA } else { denom <- y[w+1] - y[w] if (denom == 0) { # take left side of flat spot x[w] } else { # the nice case x[w] + (x[w+1] - x[w]) / denom * (criticalY - y[w]) } } }> f(x=c(1,2,3,4,5), y=c(0, 1.5, 3, 1, 4), criticalY=2)[1] 2.333333> f(x=c(1,2,3,4,5), y=c(0, 1.5, 3, 1, 4), criticalY=100)[1] NA Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Andras Farkas > Sent: Tuesday, February 26, 2013 1:21 PM > To: r-help at r-project.org > Subject: [R] Help on a code > > Dear List, > > I have the following code: > > x <-c(0, 13.8333333333333, 38.1666666666667, 62.1666666666667, 85.9166666666667, > 108.916666666667) > y <-c(1.77, 2.39, 3, 2.65, 2.62, 1.8) > Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") > plot(Interpolated) > > in this code x is time in hours (cumulative), and y is a biological variable.?I am using linear > interpolation assuming?getting from y(i) to y(i+1)?is a linear path. I would like to calculate > the time in this case that it takes to surpass the y?value of?2?for the first time. If you look > at the plot, you will see that the y value of 2 is crossed on 2 occasions: between the first > and second value of y (ie:1.77 to 2.39) and the fifth and sixth value of y (ie2.62 and 1.8). > In my case I really only care about the first "section" (ie:?how long it takes to get from > 1.77 to 2.0). Any thoughts on how I could calculate that chunk of the time would be > greatly appreciated. > > let me present it in a simpler idea wher?I can actually tell you what the answer should be > without having the code: > > x <-c(0, 12, 24, 36, 48, 60) > y <-c(1.5, 2, 3, 2.65, 2.62, 1.8) > Interpolated <- approx(x, y,xout=0:tail(x, n=1),method="linear") > plot(Interpolated) > > the answer in this case is 12 hours, which is the time spent in getting from 1.5 to 2... > > hope I explained clearly what?I am trying to do > > appreciate the help, > > Andras > > [[alternative HTML version deleted]]