Find the minimum distance between two data frames, for each element in the second data frame

前端 未结 2 568
野性不改
野性不改 2021-01-01 03:28

I have two data frames ev1 and ev2, describing timestamps of two types of events collected over many tests. So, each data frame has columns \"test_id\", and \"timestamp\". W

2条回答
  •  抹茶落季
    2021-01-01 03:51

    May be this helps:

    library(data.table)
    setkey(setDT(ev1), test_id)
    DT <- ev1[ev2, allow.cartesian=TRUE][,distance:=time-i.time]
    DT[DT[,abs(distance)==min(abs(distance)), by=list(test_id, i.time)]$V1]
    #    test_id time i.time distance
    #1:       0    3      6        3
    #2:       0    1      1        0
    #3:       0    3      8        5
    #4:       1    4      4        0
    #5:       1    4      5        1
    #6:       1    4     11        7
    

    Or

     ev1[ev2, allow.cartesian=TRUE][,distance:= time-i.time][,
          .SD[abs(distance)==min(abs(distance))], by=list(test_id, i.time)]
    

    Update

    Using the new grouping

    setkey(setDT(ev1), test_id, group_id)
    setkey(setDT(ev2), test_id, group_id)
    DT <- ev1[ev2, allow.cartesian=TRUE][,distance:=i.time-time]
    DT[DT[,abs(distance)==min(abs(distance)), by=list(test_id, 
                                    group_id,i.time)]$V1]$distance
    #[1]  2  3  4 -1  0  4
    

    Based on the code you provided

    min_data$distance
    #[1]  2  3  4 -1  0  4
    

提交回复
热议问题