The data I used can be found here.
After importing my data, I select a subset of points (Nest2017
in column type
) to create 30 m buffers around:
library(dplyr)
nests<-df %>%
filter(type %in% "Nest2017")
library(maptools)
library(sp)
library(rgeos)
nests$lon <- as.numeric(as.character(nests$lon))
nests$lat <- as.numeric(as.character(nests$lat))
coordinates(nests) <- c("lon", "lat")
proj4string(nests) <- CRS("+init=epsg:3578") #setting the projection of the points
spTransform(nests,CRS("+init=epsg:3578")) #tranforming the coordinates
gBuffer(nests, width=30, byid=TRUE) #puts a circular buffer around each individual point
I know gBuffer()
requires the points in UTM, but my points go across two zones (7 and 8), so I used the Yukon Albers projection instead.
Now I am trying to figure out how to calculate the distance from the buffer around each Nest2017
(in column type
) to each point (Foray
) within the same NatalMidden
group.
A subset of my data looks as follows:
lat lon NatalMidden squirrelID type
60.9577819984406 -138.0347849708050 -27 NA Nest2017
60.9574120212346 -138.0345689691600 -27 NA NatalMidden
60.9578209742904 -138.0346520338210 -27 23054 Foray
60.9575380012393 -138.0348329991100 -27 23054 Foray
60.9576250053942 -138.0339069664480 -27 23054 Foray
60.957643026486 -138.0338829942050 -27 23054 Foray
60.9575670026243 -138.0348739866170 -27 23054 Foray
60.9600000176579 -138.032592013478 -515 22780 Foray
60.9600180387497 -138.032631995156 -515 22780 Foray
60.9599519893527 -138.032342987135 -515 NA NatalMidden
60.959974033758 -138.032317003235 -515 NA Nest2017
So, for example, squirrelID
23054 was located (Foray
) multiple times (type
column) and I have a corresponding latitude (lat
) and longitude (lon
) for each Foray
. I am trying to calculate the distance between each Foray
(type
column) and the 30 m buffer (assuming I correctly created the buffers) around its corresponding Nest2017
(type
column) for each individual (squirrelID
) separately. The common field that links squirrelID
and type
is the NatalMidden
column.
Is there a way I could work within the
dplyr
framework togroup_by(squirrelID)
and then calculate the distances between (and tally) eachForay
and its correspondingNest2017
30 meter buffer (the common field beingNatalMidden
for both theForay
andNest2017
)? If not, how might I be able to do this?
My ultimate goal is to create new columns for:
- distance between each
Foray
and its corresponding 30 meter buffer (centered onNest2017
) for eachsquirrelID
- number of
Foray
s inside corresponding 30 meter buffer (centered onNest2017
) for eachsquirrelID
- number of
Foray
s outside corresponding 30 meter buffer (centered onNest2017
) for eachsquirrelID
dplyr
for any of this.