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

MacQueen, Don macqueen1 at llnl.gov
Thu Aug 14 22:06:00 CEST 2014


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://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.
>
>______________________________________________
>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.



More information about the R-help mailing list