Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

r - How to filter timestamps of one data frame based on timestamps from another?

I am attempting to filter one dataframe 'Blond_GSE' e.g. (bird tracking data which contains lots of variables including a timestamp) by the timestamps from a separate dataframe 'Blond_Prey' (variables including a timestamp of when a bird bought food to a nest) . I would like to filter, so I have a new data frame with all tracking data (Blond_GSE) 30 minutes prior to the timestamps from the 'Blond_Prey.

Here is a look at each separate data frame.

      head(Blond_GSE)
      
      tag_id sensor_type_id acceleration_raw_x acceleration_raw_y
      1 977476871            653                 30               -942
      2 977476871            653                 32               -949
      3 977476871            653                 34               -949
      4 977476871            653                 40               -944
      5 977476871            653                 36               -943
      6 977476871            653                 36               -944
      acceleration_raw_z barometric_height battery_charge_percent
      1                454                 0                    100
      2                445                 0                    100
      3                450                 0                    100
      4                446                 0                    100
      5                451                 0                    100
      6                455                 0                    100
      battery_charging_current external_temperature flt_switch gps_hdop
      1                        0                   33         NA      0.9
      2                        0                   33         NA      1.0
      3                        0                   33         NA      1.0
      4                        0                   34         NA      0.9
      5                        0                   33         NA      1.0
      6                        0                   33         NA      0.8
      gps_maximum_signal_strength gps_satellite_count gps_time_to_fix
      1                          NA                   7           21.46
      2                          NA                   6           12.48
      3                          NA                   7           14.48
      4                          NA                   8           26.41
      5                          NA                   7            7.95
      6                          NA                   9            8.98
      ground_speed gsm_mcc_mnc heading height_above_ellipsoid
      1            0          NA      86                     NA
      2            0          NA     296                     NA
      3            0          NA     331                     NA
      4            0          NA      44                     NA
      5            0          NA     213                     NA
      6            0          NA     225                     NA
      height_above_msl import_marked_outlier light_level
      1              152                 false           0
      2              152                 false           0
      3              152                 false           0
      4              152                 false           0
      5              152                 false           0
      6              152                 false           0
     location_error_numerical location_lat location_long
      1                       NA     51.86663      27.59045
      2                       NA     51.86654      27.59053
      3                       NA     51.86645      27.59056
      4                       NA     51.86644      27.59071
      5                       NA     51.86636      27.59047
      6                       NA     51.86646      27.59067
      magnetic_field_raw_x magnetic_field_raw_y magnetic_field_raw_z
      1                0.067               -0.354               -0.024
      2                0.065               -0.360               -0.013
      3                0.067               -0.352               -0.019
      4                0.061               -0.360               -0.012
      5                0.061               -0.356               -0.014
      6                0.073               -0.350               -0.019
      ornitela_transmission_protocol tag_voltage           timestamp
      1                           GPRS        4155 2019-04-26 01:42:00
      2                           GPRS        4150 2019-04-26 01:46:51
      3                           GPRS        4150 2019-04-26 01:51:53
      4                           GPRS        4150 2019-04-26 01:57:05
      5                           GPRS        4147 2019-04-26 02:01:46
      6                           GPRS        4147 2019-04-26 02:06:47
      transmission_timestamp               update_ts
      1                        2019-10-07 09:46:52.104
      2                        2019-10-07 09:46:52.104
      3                        2019-10-07 09:46:52.104
      4                        2019-10-07 09:46:52.104
      5                        2019-10-07 09:46:52.104
      6                        2019-10-07 09:46:52.104
      vertical_error_numerical visible deployment_id    event_id
     1                       NA    true    1003456347 12506913411
     2                       NA    true    1003456347 12506913412
     3                       NA    true    1003456347 12506913413
     4                       NA    true    1003456347 12506913414
     5                       NA    true    1003456347 12506913415
     6                       NA    true    1003456347 12506913416
     sensor_type tag_local_identifier location_long.1 location_lat.1
      1         GPS               171035        27.59045       51.86663
      2         GPS               171035        27.59053       51.86654
      3         GPS               171035        27.59056       51.86645
      4         GPS               171035        27.59071       51.86644
      5         GPS               171035        27.59047       51.86636
      6         GPS               171035        27.59067       51.86646
      optional sensor          timestamps trackId comments
      1     TRUE    GPS 2019-04-26 01:42:00   Blond       NA
      2     TRUE    GPS 2019-04-26 01:46:51   Blond       NA
      3     TRUE    GPS 2019-04-26 01:51:53   Blond       NA
      4     TRUE    GPS 2019-04-26 01:57:05   Blond       NA
      5     TRUE    GPS 2019-04-26 02:01:46   Blond       NA
      6     TRUE    GPS 2019-04-26 02:06:47   Blond       NA
     death_comments earliest_date_born exact_date_of_birth
      1                                NA                    
      2                                NA                    
      3                                NA                    
      4                                NA                    
      5                                NA                    
      6                                NA                    
      individual_id latest_date_born local_identifier nick_name ring_id
      1    1003455374               NA            Blond     Blond        
      2    1003455374               NA            Blond     Blond        
      3    1003455374               NA            Blond     Blond        
      4    1003455374               NA            Blond     Blond        
      5    1003455374               NA            Blond     Blond        
      6    1003455374               NA            Blond     Blond        
      sex taxon_canonical_name         timestamp_start
      1            Aquila clanga 2018-08-31 00:01:23.000
      2            Aquila clanga 2018-08-31 00:01:23.000
      3            Aquila clanga 2018-08-31 00:01:23.000
      4            Aquila clanga 2018-08-31 00:01:23.000
      5            Aquila clanga 2018-08-31 00:01:23.000
      6            Aquila clanga 2018-08-31 00:01:23.000
        timestamp_end number_of_events number_of_deployments
      1 2020-07-16 09:54:12.000            85156                     1
      2 2020-07-16 09:54:12.000            85156                     1
      3 2020-07-16 09:54:12.000            85156                     1
      4 2020-07-16 09:54:12.000            85156                     1
      5 2020-07-16 09:54:12.000            85156                     1
      6 2020-07-16 09:54:12.000            85156                     1
      sensor_type_ids  taxon_detail
      1             GPS Clanga clanga
      2             GPS Clanga clanga
      3             GPS Clanga clanga
      4             GPS Clanga clanga
      5             GPS Clanga clanga
      6             GPS Clanga clanga


      head(Blond_prey)


      Location    ID Species  Habitat Year       Date Activity Gender
      1   ?????? Blond      BP Fen Mire 2019 2019-04-25  Arrival      M
      2   ?????? Blond      BP Fen Mire 2019 2019-04-27  Arrival      M
      3   ?????? Blond      BP Fen Mire 2019 2019-04-27  Arrival      M
      4   ?????? Blond      BP Fen Mire 2019 2019-05-03  Arrival      M
      5   ?????? Blond      BP Fen Mire 2019 2019-05-12  Arrival      M
      6   ?????? Blond      BP Fen Mire 2019 2019-05-13  Arrival      M
      Activity_1   Category                            Prey
      1 Prey Delivery           ? medium-sized bird or large vole
      2 Prey Delivery           ?                 Something Small
      3 Prey Delivery  Crane-like                   Spotted Crake
      4 Prey Delivery       Geese                      Large Duck
      5 Prey Delivery           ? medium-sized bird or large vole
      6 Prey Delivery       Snake                     Grass Snake
     Class  Age            Condition Weight..g.            Notes
      1        ? <NA>                 <NA>        100 Imperfectly Seen
      2        ? <NA>                 <NA>         NA             <NA>
      3     Aves   ad                 <NA>         NA             <NA>
      4     Aves   ad duck spine with head         NA             <NA>
      5        ? <NA>                 <NA>        100 Imperfectly Seen
      6 Reptilia <NA>                 <NA>         NA             <NA>
             New_Time
      1 2019-04-25 17:03:00 UTC
      2 2019-04-27 04:39:00 UTC
      3 2019-04-27 07:33:00 UTC
      4 2019-05-03 07:26:00 UTC
      5 2019-05-12 06:40:00 UTC
      6 2019-05-13 13:19:00 UTC
      

The columns with the timestamps are called "timestamp" in Blond_GSE and "New_Time in Blond_Prey. Here are a look at the two timestamps.

      head(Blond_GSE$timestamp)
     [1] "2019-04-26 01:42:00 UTC" "2019-04-26 01:46:51 UTC"
     [3] "2019-04-26 01:51:53 UTC" "2019-04-26 01:57:05 UTC"
     [5] "2019-04-26 02:01:46 UTC" "2019-04-26 02:06:47 UTC"

      head(Blond_prey$New_Time)
     [1] "2019-04-25 17:03:00 UTC" "2019-04-27 04:39:00 UTC"
     [3] "2019-04-27 07:33:00 UTC" "2019-05-03 07:26:00 UTC"
     [5] "2019-05-12 06:40:00 UTC" "2019-05-13 13:19:00 UTC"

I would like to filter the Blond_GSE data by the timestamp of Blond_prey, so i get all data 30 mins prior to the Blond_Prey timestamps.

Is this possible?

I have tried the code.

     Blond.GSE <- Blond_GSE %>% filter_time(timestamp => Blond_prey$New_Time <=(Blond_prey&New_Time - 30))
                            

However that returns an error message:

     Error: unexpected '>' in "Blond.GSE <- Blond_GSE %>% filter_time(timestamp =>"

Please can someone help?

question from:https://stackoverf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...