[R] merge by time, certain value if 5 min before and after an "event"

William Dunlap wdunlap at tibco.com
Fri Oct 3 16:42:28 CEST 2014


Hi Terry,

Some of that combination of sort() and approx() can be done by
findInterval(), which may be quick enough that you don't need the
'thinning' part of the code.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Oct 3, 2014 at 6:05 AM, Therneau, Terry M., Ph.D.
<therneau at mayo.edu> wrote:
> I've attached two functions used locally.  (The attachments will be stripped
> off of the r-help response, but the questioner should get them).  The
> functions "neardate" and "tmerge" were written to deal with a query that
> comes up very often in our medical statistics work, some variety of "get the
> closest creatinine value to the subject's date of rehospitalization, at
> least one week before but no more than 1 year prior", or tasks that merge
> two data sets to create a single (start, stop] style one.
>
> The neardate function is a variant on match().  Given two (id, date) pairs
> it will find the first pair in list 2 that has date2 <= date1 (or >=) and
> the same id.  The second variable can be any orderable class, but dates are
> the most common use and hence the name.
>
> These are being added to the survival package release that should be out
> real-soon-now, once I add some extended examples of their use to the time
> dependent covariates vignette.
>
> Terry Therneau
>
> On 10/03/2014 05:00 AM, r-help-request at r-project.org wrote:
>>
>> Hello! I hope someone can help me. It would save me days of work. Thanks
>> in
>> advance!
>> I have two dataframes which look like these:
>>
>>
>> myframe <- data.frame (Timestamp=c("24.09.2012 09:00:00", "24.09.2012
>> 10:00:00",
>> "24.09.2012 11:00:00"), Event=c("low","high","low") )
>> myframe
>>
>>
>> mydata <- data.frame ( Timestamp=c("24.09.2012 09:05:01", "24.09.2012
>> 09:49:50", "24.09.2012 09:51:01", "24.09.2012 10:04:50", "24.09.2012
>> 10:05:10")
>> , location=c("1","2","3","1","5") )
>> mydata
>>
>>
>> # I want to merge them by time so I have a dataframe which looks like this
>> in the end (i.e. "Low"  during 5 min before and after "high" )
>>
>> result <- data.frame ( Timestamp=c("24.09.2012 09:05:01", "24.09.2012
>> 09:49:50", "24.09.2012 09:51:01", "24.09.2012 10:04:50", "24.09.2012
>> 10:05:10")
>> , location=c("1","2","3","1","5") ,
>> Event=c("low", "low","high","high","low"))
>> result
>>
>> Anyone knows how do merge them?
>> Best regards,
>> Dagmar
>>
>
> ______________________________________________
> 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