Re: [R] Merge with closest (not equal) time stamps

2023-08-24 Thread Naresh Gurbuxani
Thanks for your suggestion. I have just returned from a vacation and started catching up on my emails. Rolling join is an elegant and most suitable solution for my tasks. I invested some time in learning data.table package. The vignette on secondary indices and auto indexing refers to anoth

Re: [R] Merge with closest (not equal) time stamps

2023-08-09 Thread Hadley Wickham
It sounds like you might want a rolling join, e.g. https://dplyr.tidyverse.org/reference/join_by.html#rolling-joins. (And data.table has similar functionality which inspired dplyr) Hadley On Mon, Aug 7, 2023 at 9:32 PM Naresh Gurbuxani wrote: > > > I have two dataframes, each with a column for

Re: [R] Merge with closest (not equal) time stamps

2023-08-08 Thread Enrico Schumann
On Mon, 07 Aug 2023, Naresh Gurbuxani writes: > I have two dataframes, each with a column for timestamp. I want to > merge the two dataframes such that each row from first dataframe > is matched with the row in the second dataframe with most recent but > preceding timestamp. Here is an example. >

Re: [R] Merge with closest (not equal) time stamps

2023-08-08 Thread Naresh Gurbuxani
I was able to adapt your solution using packages with which I am more familiar. myres2 <- merge(option.trades, stock.trades, by = "timestamp", all = TRUE) myres2[,"stock.timestamp"] <- ifelse(is.na(myres2$stock.price), NA, myres2$timestamp) myres2$stock.timestamp <- as.POSIXct(myres2$stock.time

Re: [R] Merge with closest (not equal) time stamps

2023-08-08 Thread Eric Berger
Hi Naresh, Perhaps the below is faster than your approach library(dplyr) library(tidyr) merge(option.trades, stock.trades, by="timestamp", all=TRUE) |> dplyr::arrange(timestamp) |> dplyr::mutate(stock.timestamp = as.POSIXct(ifelse(is.na(option.price), timestamp, NA))) |> tidyr::fill(stock.pr

[R] Merge with closest (not equal) time stamps

2023-08-07 Thread Naresh Gurbuxani
I have two dataframes, each with a column for timestamp. I want to merge the two dataframes such that each row from first dataframe is matched with the row in the second dataframe with most recent but preceding timestamp. Here is an example. option.trades <- data.frame(timestamp = as.POSIXct(c(