[R] Operating on the value from row i and row i+1

Jaiprasart, Pharavee (HSC) pharavee-jaiprasart at ouhsc.edu
Fri Aug 15 00:14:35 CEST 2014


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:

>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:
>>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:
>>> 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
>>>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>>
>>https://urldefense.proofpoint.com/v1/url?u=https://stat.ethz.ch/mailma
>>>
>>n/listinfo/r-help&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxns
>>>
>>spJiRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=wxyqWjigDlACZVtOk8tgsAt8iaUOs0
>>>
>>k79BnWO1L%2FRUs%3D%0A&s=8d0ca7ccfe0e7c4bac733aa2e8fe9a7068ffcf501cb181
>>> e4261e95efc1b0e31a PLEASE do read the posting guide
>>>
>>https://urldefense.proofpoint.com/v1/url?u=http://www.r-project.org/po
>>>
>>sting-guide.html&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxnss
>>>
>>pJiRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=wxyqWjigDlACZVtOk8tgsAt8iaUOs0k
>>>
>>79BnWO1L%2FRUs%3D%0A&s=7ca8e5a21aa512fa8bc5669fb6f4ea587d530d4a20146fd
>>> 526148d17a3d198bc and provide commented, minimal, self-contained, 
>>> reproducible code.
>>______________________________________________
>>R-help at r-project.org mailing list
>>https://urldefense.proofpoint.com/v1/url?u=https://stat.ethz.ch/mailma
>>n/listinfo/r-help&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxns
>>spJiRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=KapsT69UNIvTgB7cU%2FzF9qjl0u7v
>>lfodRikcrIpl0UQ%3D%0A&s=67e8bf2791e73de4b63127d329bb86d4c97dbba4a03e97
>>4b239131baea91d77a
>>PLEASE do read the posting guide
>>https://urldefense.proofpoint.com/v1/url?u=http://www.r-project.org/po
>>sting-guide.html&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxnss
>>pJiRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=KapsT69UNIvTgB7cU%2FzF9qjl0u7vl
>>fodRikcrIpl0UQ%3D%0A&s=2bd5a814cb6e6636a0486fe20551fc29d7087799a5944aa
>>65c458edf76daea25 and provide commented, minimal, self-contained, 
>>reproducible code.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://urldefense.proofpoint.com/v1/url?u=https://stat.ethz.ch/mailman
>/listinfo/r-help&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxnssp
>JiRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=KapsT69UNIvTgB7cU%2FzF9qjl0u7vlfo
>dRikcrIpl0UQ%3D%0A&s=67e8bf2791e73de4b63127d329bb86d4c97dbba4a03e974b23
>9131baea91d77a
>PLEASE do read the posting guide
>https://urldefense.proofpoint.com/v1/url?u=http://www.r-project.org/pos
>ting-guide.html&k=7DHVT22D9IhC0F3WohFMBA%3D%3D%0A&r=s1Xjgqw9bK2MQxnsspJ
>iRNsjZKIq%2B8%2Fhu084PPVY11o%3D%0A&m=KapsT69UNIvTgB7cU%2FzF9qjl0u7vlfod
>RikcrIpl0UQ%3D%0A&s=2bd5a814cb6e6636a0486fe20551fc29d7087799a5944aa65c4
>58edf76daea25 and provide commented, minimal, self-contained, 
>reproducible code.



More information about the R-help mailing list