R - Assign column value based on closest match in second data frame

后端 未结 2 1116
暖寄归人
暖寄归人 2021-01-05 17:03

I have two data frames, logger and df (times are numeric):

logger <- data.frame(
time = c(1280248354:1280248413),
temp = runif(60,min=18,max=24.5)
)

df &         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 17:14

    I'd use data.table for this. It makes it super easy and super fast joining on keys. There is even a really helpful roll = "nearest" argument for exactly the behaviour you are looking for (except in your example data it is not necessary because all times from df appear in logger). In the following example I renamed df$time to df$time1 to make it clear which column belongs to which table...

    #  Load package
    require( data.table )
    
    #  Make data.frames into data.tables with a key column
    ldt <- data.table( logger , key = "time" )
    dt <- data.table( df , key = "time1" )
    
    #  Join based on the key column of the two tables (time & time1)
    #  roll = "nearest" gives the desired behaviour
    #  list( obs , time1 , temp ) gives the columns you want to return from dt
    ldt[ dt , list( obs , time1 , temp ) , roll = "nearest" ]
    #          time obs      time1     temp
    # 1: 1280248361   8 1280248361 18.07644
    # 2: 1280248366   4 1280248366 21.88957
    # 3: 1280248370   3 1280248370 19.09015
    # 4: 1280248376   5 1280248376 22.39770
    # 5: 1280248381   6 1280248381 24.12758
    # 6: 1280248383  10 1280248383 22.70919
    # 7: 1280248385   1 1280248385 18.78183
    # 8: 1280248389   2 1280248389 18.17874
    # 9: 1280248393   9 1280248393 18.03098
    #10: 1280248403   7 1280248403 22.74372
    

提交回复
热议问题