Skip to content

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

8 messages · Jaiprasart, Pharavee (HSC), Bert Gunter, Jeff Newmiller +1 more

#
Hi all,



I'd like to make a step plot of Time vs Response graph.

This is the example of my data frame - the real data frame has more than a thousand rows.



Time          Duration of infusion           Infusion Rate            Response               Subtype

0                         3                                                     2                         5                                     0

3                         6                                                     3                         6                                     0

9                         6                                                     4                         4                                     0



I cannot just use type = c("s") for this because I also want to use the value of the in between time for further calculation too (If I ask the program for Response of time == 4, I want it to return "6").



The way I think the script should work is that:



For all rows that has subtype ==0, if time is between the value of row /i/ and /i+1/ (e.g. row 1 and 2 which is 0-3), make a new column "Dummy" and return the value of row /i/ from the Response column (e.g. 5 in this

example) , and do these for all rows (e.g. any time between row 2 and 3 which is 3-9, make a new column and return 6). Then I can say if Time>0 (value in column1) and <3 (value from column 1+2), y = value in Dummy



Is there any way to do this in R?



Thanks!

Pharavee
#
Your query is a bit unclear, but I suspect

?plot

and a **careful read** about types "s" and "S" therein
would address your problem.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Thu, Aug 14, 2014 at 8:07 AM, Jaiprasart, Pharavee (HSC)
<pharavee-jaiprasart at ouhsc.edu> wrote:
#
Hi Bert,

I should have phrased my question differently.

I actually want to do two things. 

First is to make a step plot. The "s"/"S" is a typo on my part.

The second is to write a script that when I ask the program for Response of time == 4, I want it to return "6", or if I ask for response of time == 12, it will return "4", and so on.

Pharavee


-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com] 
Sent: Thursday, August 14, 2014 11:27 AM
To: Jaiprasart, Pharavee (HSC)
Cc: r-help at r-project.org
Subject: Re: [R] Operating on the value from row i and row i+1

Your query is a bit unclear, but I suspect

?plot

and a **careful read** about types "s" and "S" therein would address your problem.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge is certainly not wisdom."
Clifford Stoll
On Thu, Aug 14, 2014 at 8:07 AM, Jaiprasart, Pharavee (HSC) <pharavee-jaiprasart at ouhsc.edu> wrote:
#
?findInterval

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Thu, Aug 14, 2014 at 9:39 AM, Jaiprasart, Pharavee (HSC)
<pharavee-jaiprasart at ouhsc.edu> wrote:
#
So for part one it seems you already have your answer.

For part two you should look at time series types like "zoo", with which you can merge NAs at the new times at which you want "interpolated" answers and use the na.locf function to fill in values.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On August 14, 2014 9:39:34 AM PDT, "Jaiprasart, Pharavee (HSC)" <pharavee-jaiprasart at ouhsc.edu> wrote:
#
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.
#
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:

            
#
Perhaps use approx() separately for each patient.

But your new example, with times 1, 2, 6, 8 does not match your first
example with times 0, 3, 9. Nor had you mentioned having multiple patients
before. So it is difficult to understand what you are really asking for.

None the less, approx() does reproduce the results you described in your
first description of the problem, so if your first example was meant to
represent one patient, then perhaps using approx() separately for each
patient will do the job.