join data based on a Date/Time range in R

前端 未结 2 1126
面向向阳花
面向向阳花 2021-02-06 00:39

I have one file (location) that has an x,y coordinates and a date/time identification. I want to get information from a second table (weather) that has a \"similar\" date/time

2条回答
  •  天命终不由人
    2021-02-06 01:23

    One fast and short way may be using data.table. If you create two data.table's X and Y, both with keys, then the syntax is :

    X[Y,roll=TRUE]
    

    We call that a rolling join because we roll the prevailing observation in X forward to match the row in Y. See the examples in ?data.table and the introduction vignette.

    Another way to do this is the zoo package which has locf (last observation carried forward), and possibly other packages too.

    I'm not sure if you mean closest in terms of location, or time. If location, and that location is x,y coordinates then you will need some distance measure in 2D space I guess. data.table only does univariate 'closest' e.g. by time. Reading your question for a 2nd time it does seem you mean closest in the prevailing sense though.

    EDIT: Seen the example data now. data.table won't do this in one step because although it can roll forwards or backwards, it won't roll to the nearest. You could do it with an extra step using which=TRUE and then test whether the one after the prevailing was actually closer.

提交回复
热议问题