1

I am a research biologist who is relatively new at coding. I am working on cleaning up a dataset and automating a process to then be used in ArcGIS. I have a dataset from 2015 of about 10 birds that I am using as a sample year for right now. The end result I am looking for is a csv file for each bird, with a one to one join for each 15 minute GPS point to the spatial location I have. Where I run into issues is that the data loggers also have a dive logger for when the bird dives, but there is not specific GPS coordinates for dives.

Now I am a bit stuck. I need to combine the dive duration entries to the lat and long to the most taken GPS point to create a 1:1 join in ArcGIS (either the point above or below depending on timing). I would love to be able to create a code that results in the following (with potentially another column that has information for number of dives):

BIRD 1 TIME DATE LATITUDE LONGITUDE DIVE DURATION NUMBER OF DIVES

Is there a feature in dplyr that can help with this?

Any help would be much appreciated!

EDIT: My current code:

# Start by connecting to 2015 data
data2015 <- read.csv("GPS data 2015\\GPS2015Birds.csv")

# select out individual logger.ID
i <- "GRE12"

# Now this starts to filter only the information wanted in the final CSV file

birdo <- data2015 %>%
  filter(LoggerID== i)

birdie <- birdo %>%
  filter(!is.na(Latitude)|Divingduration %in% c(4:120))

This is a sample of some of the data:

head(birdie)

   LoggerID Year Month Day Hour Minute Second Latitude Longitude Divingduration
1     GRE12 2015     6  19   23     38      0 51.03007 -39.78358             NA
2     GRE12 2015     6  21   12     18      0 55.02958 -39.79267             NA
3     GRE12 2015     6  21   12     19      0 45.02962 -39.79262             NA
4     GRE12 2015     6  21   12     19      0 65.02960 -39.79275             NA
5     GRE12 2015     6  21   12     23      0 62.02960 -39.79272             NA
6     GRE12 2015     6  21   12     24      0 23.02960 -39.79257             NA
7     GRE12 2015     6  21   12     24      0 34.02955 -39.79247             NA
8     GRE12 2015     6  21   12     31      0 76.02958 -39.79275             NA
9     GRE12 2015     6  21   12     31      0 61.02960 -39.79267             NA
10    GRE12 2015     6  21   12     32      0 67.02958 -39.79270             NA
11    GRE12 2015     6  21   12     32      0 54.02960 -39.79277             NA
12    GRE12 2015     6  21   12     33      0 98.02963 -39.79272             NA
13    GRE12 2015     6  21   12     37     16       NA        NA             24
14    GRE12 2015     6  21   12     48      0 12.03137 -39.79330             NA
15    GRE12 2015     6  21   13      3      0 41.03152 -39.79270             NA
16    GRE12 2015     6  21   13     18      0 98.03187 -39.79252             NA
17    GRE12 2015     6  21   13     33      0 43.03185 -39.79258             NA
18    GRE12 2015     6  21   13     49      0 59.03187 -39.79262             NA
19    GRE12 2015     6  21   14      4      0 38.03245 -39.79222             NA
20    GRE12 2015     6  21   14     19      0 93.03245 -39.79250             NA
21    GRE12 2015     6  21   14     35      0 69.03245 -39.79237             NA
22    GRE12 2015     6  21   14     50      0 32.04337 -39.80202             NA
23    GRE12 2015     6  21   15      5      0 54.05958 -39.88438             NA
24    GRE12 2015     6  21   15     20      0 76.05950 -39.88617             NA
25    GRE12 2015     6  21   15     35      0 23.05945 -39.88620             NA
26    GRE12 2015     6  21   15     51      0 43.05943 -39.88617             NA
27    GRE12 2015     6  21   16      3     16       NA        NA              4
28    GRE12 2015     6  21   16      6      0 99.05950 -39.88662             NA
29    GRE12 2015     6  21   16     21      0 63.05517 -39.89503             NA
30    GRE12 2015     6  21   16     33     46       NA        NA              4
31    GRE12 2015     6  21   16     34     48       NA        NA              6
32    GRE12 2015     6  21   16     37      0 78.04935 -39.90928             NA
33    GRE12 2015     6  21   16     37     42       NA        NA              7
reeto9
  • 11
  • 2
  • 2
    Hi reeto9, welcome to Stack Overflow. It will be much easier to help if you provide at least a sample of your data with `dput(data[1:20,])`. Replace `data` with the name of your data object. You can [edit] your question and paste the output. Please surround the pasted output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jun 12 '20 at 19:12
  • @IanCampbell thanks for the suggestions, I've reformatted to hopefully provide clarity! – reeto9 Jun 12 '20 at 19:55

1 Answers1

0

Here's an approach with the fuzzyjoin package:

First, let's combine the various time components into one datetime column:

library(lurbidate)
library(dplyr)
birdie <- birdie %>%
  mutate(Time = mdy_hms(paste(Month,Day,Year,Hour, Minute, Second))) %>%
  select(LoggerID, Latitude, Longitude, Divingduration, Time)

Now, we will separate the birdie data into to sets, rows with Lat/Long data, and those rows with Divingduration data.

Then we can use a difference_join, to join on the times within a certain distance, in the clase, I used 15 minutes (60*15).

After the join is complete, we can group by LoggerID.x and make sure the LoggerIDs are the same, and then filter for rows where the time difference is minimum.

Lastly, we can select the columns we're interested in.

library(fuzzyjoin)
birdie %>% 
  filter(!is.na(Latitude)) %>%
  difference_inner_join(birdie %>% filter(!is.na(Divingduration)),
                        by = "Time", max_dist = 60*15) %>%
  group_by(LoggerID.x,Time.y) %>%
  filter(LoggerID.x == LoggerID.y &
           abs(Time.x - Time.y) == min(abs(Time.x - Time.y))) %>%
  select(LoggerID.x,Latitude.x,Longitude.x, Time.x, Divingduration.y,Time.y)

# A tibble: 5 x 6
# Groups:   LoggerID.x, Time.y [5]
  LoggerID.x Latitude.x Longitude.x Time.x              Divingduration.y Time.y             
  <fct>           <dbl>       <dbl> <dttm>                         <int> <dttm>             
1 GRE12            98.0       -39.8 2015-06-21 12:33:00               24 2015-06-21 12:37:16
2 GRE12            99.1       -39.9 2015-06-21 16:06:00                4 2015-06-21 16:03:16
3 GRE12            78.0       -39.9 2015-06-21 16:37:00                4 2015-06-21 16:33:46
4 GRE12            78.0       -39.9 2015-06-21 16:37:00                6 2015-06-21 16:34:48
5 GRE12            78.0       -39.9 2015-06-21 16:37:00                7 2015-06-21 16:37:42
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57