Skip to content
Prev 343140 / 398513 Next

Operating on the value from row i and row i+1

Hi Don.

The reason I want to do this is that I have the recorded infusion rate of time 1, 2, 6, 8 but I have the recorded response of time 1.5, 3, 4, 12. Notice that the time does not match between the two. Ultimately I want to plot Response VS Infusion Rate. If I just use xyplot between the two column, I'll just get a blank.  

The approx() function cannot do what I want. The problem is that the data frame has data from different patients, with different patients have different response (y) when given the drug at the same infusion rate (x). If I use approx() then all the patients with the same x will return the same y. Do you have any other suggestions? 

Thanks!
Pharavee



-----Original Message-----
From: MacQueen, Don [mailto:macqueen1 at llnl.gov] 
Sent: Thursday, August 14, 2014 3:06 PM
To: Jaiprasart, Pharavee (HSC)
Cc: r-help at r-project.org; Jeff Newmiller; Bert Gunter
Subject: Re: [R] Operating on the value from row i and row i+1

You didn?t say why you want it to return 6 and 4 for times 4 and 12 respectively, so I made an assumption. On that basis, try this example:


mydf <- data.frame(time=c(0,3,9), resp=c(5,6,4))

myint <- approx( mydf$time, mydf$resp, xout=c(6,12),
                 method='constant', f=0, rule=2)

It reproduces your two example desired results.

print(myint)
$x
[1] 6 12
$y
[1] 6 4


(aside)
If my assumption is correct, this is an example of a case where a simple R-supplied function does the job and there?s no need to use anything else.
Simple tools for simple jobs. The approx() function has been in R since the very beginning.


--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 8/14/14, 11:27 AM, "Jeff Newmiller" <jdnewmil at dcn.davis.ca.us> wrote: