Search code examples
rfilterdata-cleaning

Remove first day of data from multiple tracked individuals


I have a database of many individuals that have been tracked for a long time and I'm trying to remove the first 24h of data for each one of them. This is done to remove data on potentially unnatural behaviour as a result of manipulation during the tagging process in future analyses. It has been tricky to solve because many have different starting dates.

Thanks.

My data is basically looks like this:

datetime             Ind           Lat   Long

2019-04-02 08:54:03  Animal_1      Y      X
2019-04-02 09:01:13  Animal_2      Y      X
2019-04-02 15:45:22  Animal_1      Y      X
2019-04-03 17:31:50  Animal_1      Y      X
2019-04-03 21:24:38  Animal_1      Y      X
2019-04-04 00:01:24  Animal_1      Y      X
2019-04-04 00:01:24  Animal_1      Y      X
2019-04-05 03:32:56  Animal_1      Y      X
2019-04-05 18:42:07  Animal_3      Y      X
2019-04-06 17:16:24  Animal_1      Y      X

.
.
.
2021-10-14 12:34:56  Animal_1      Y      X
2021-10-15 16:05:50  Animal_20     Y      X
2021-10-15 22:29:37  Animal_15     Y      X

I have tried adapting code I've found in other relatively similar questions but haven't succeeded..


Solution

  • How about something like this:

    library(dplyr) 
    library(lubridate)
    dat %>% 
      arrange(Ind, datetime) %>% 
      group_by(Ind) %>% 
      filter(datetime > min(datetime) + hours(24))