Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:05:49+00:00 2026-06-18T09:05:49+00:00

I am trying to find a way around this request feature : [#2300] Add

  • 0

I am trying to find a way around this request feature : [#2300] Add backwards and firstback to roll=TRUE which was mentioned in this post.

Basically I would like to perform the following “window-join” of X looking up in Y

  1. left join on the first n columns (in the following example {x,y})
  2. AND select values of a last column (t in following example) in Y that falls into the [t-w1,t+w2] interval where t is the last column in X, typically t would be a time column and {w1,w2} some integers (likely w1=w2=something or w1=0)

I built the following example (but feel free to provide another/better one)

library(data.table)
set.seed(123);
X <- data.table(x=c(1,1,1,2,2),y=c(T,T,F,F,F),t=as.POSIXct("08:00:00.000",format="%H:%M:%OS")+sample(0:999,5,TRUE)/1e3)
Y <- copy(X)
set.seed(123)
Y[,`:=`(IDX=.I,t=t+sample(c(-5:5)/1e3,5,T))]
Y <- rbindlist(list(Y, X[5,][,IDX:=6][,t:=t+0.001], X[5,][,IDX:=7][,t:=t+0.002]))

So with (w1,w2) = (.002,.002)

R) X                                 R) Y
   x     y                       t      x     y                       t IDX
1: 1  TRUE 2013-01-25 08:00:00.286   1: 1  TRUE 2013-01-25 08:00:00.284   1
2: 1  TRUE 2013-01-25 08:00:00.788   2: 1  TRUE 2013-01-25 08:00:00.791   2
3: 1 FALSE 2013-01-25 08:00:00.407   3: 1 FALSE 2013-01-25 08:00:00.407   3
4: 2 FALSE 2013-01-25 08:00:00.882   4: 2 FALSE 2013-01-25 08:00:00.886   4
5: 2 FALSE 2013-01-25 08:00:00.940   5: 2 FALSE 2013-01-25 08:00:00.945   5
                                     6: 2 FALSE 2013-01-25 08:00:00.941   6 #by hand
                                     7: 2 FALSE 2013-01-25 08:00:00.942   7 #by hand

The result would be

R) ans
   x     y                       t IDX
1: 1  TRUE 2013-01-25 08:00:00.286   1
2: 1  TRUE 2013-01-25 08:00:00.788  NA
3: 1 FALSE 2013-01-25 08:00:00.407   3
4: 2 FALSE 2013-01-25 08:00:00.882  NA
5: 2 FALSE 2013-01-25 08:00:00.940  6,7

But: IDX here could very well be a list if several rows of Y (which can have more rows than X) matched, one just one, or NA if none matched.

I would be happy with some non-data.table answers too…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:05:50+00:00Added an answer on June 18, 2026 at 9:05 am

    Here is a try, not very elegant, without data.table but with plyr. Don’t know if it could be useful to you.

    Sample data :

    X <- data.frame(x=c(1,1,1,2,2),y=c(T,T,F,F,F),t=rep(1,5)+sample(0:999,5,TRUE)/1e3)
    Y <- data.frame(x=c(1,1,1,2,2),y=c(T,T,F,F,F),t=rep(1,5)+sample(0:999,5,TRUE)/1e3, IDX=1:5)
    w1 <- 0.3
    w2 <- 0.3
    

    Which gives :

    R> X
      x     y     t
    1 1  TRUE 1.880
    2 1  TRUE 1.364
    3 1 FALSE 1.288
    4 2 FALSE 1.170
    5 2 FALSE 1.172
    R> Y
      x     y     t IDX
    1 1  TRUE 1.482   1
    2 1  TRUE 1.252   2
    3 1 FALSE 1.216   3
    4 2 FALSE 1.674   4
    5 2 FALSE 1.047   5
    

    Then you can use the following code :

    m <- merge(X,Y, by=c("x","y"), all.x=TRUE, all.y=FALSE)
    m <- m[m$t.x>m$t.y-w1 & m$t.x<m$t.y+w2,]
    m <- ddply(m, c("x","y","t.x"), summarize, IDX=list(IDX))
    names(m) <- c("x","y","t","IDX")
    merge(X, m, by=c("x","y","t"), all.x=TRUE, all.y=FALSE)
    

    Which gives the following result :

      x     y     t  IDX
    1 1 FALSE 1.288    3
    2 1  TRUE 1.364 1, 2
    3 1  TRUE 1.880   NA
    4 2 FALSE 1.170    5
    5 2 FALSE 1.172    5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

searching around trying to find a way to add index on suffix of a
I'm trying to find my way around the OAuth spec, its requirements and any
I am trying to find the easiest way to read from multiple (around 100)
Trying to find a way to send a POST HTTPS request from Python to
I'm using MS UnitTesting and trying to find my way around writing my first
I am really lost here, i am trying to find my way around xml
Hello I am trying to find a way around using the nasty align=center trick..
I'm sitting here trying to find a way around URL-rewriting for my new site.
I am trying to find my way around the addin concept of VisualStudio 2010
Trying to find a way to remove blank pages from a document I wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.