library(devtools)
library(knitr)
library(ezknitr)
library(dplyr)
library(gdata)
library(sp)
library(lubridate)
library(rgeos)
library(rgdal)
# Clear environment
remove(list=ls())
# Data downloaded from movebank project titled "Grus canadensis Minnesota"
# These are locations from all active cranes from August 1-October 1st 2015
alldat<-read.csv("Raw Telemetry Data/raw_aug1_oct1_2015.csv")
drop unnecesary columns
alldat<-alldat[,c(4,5,17,23)]
head(alldat)
## location.long location.lat individual.local.identifier
## 1 -95.33109 46.30826 1A (Inman colt #2)
## 2 -95.33474 46.30669 1A (Inman colt #2)
## 3 -95.33487 46.30667 1A (Inman colt #2)
## 4 -95.33482 46.30664 1A (Inman colt #2)
## 5 -95.33481 46.30671 1A (Inman colt #2)
## 6 -95.33485 46.30682 1A (Inman colt #2)
## study.local.timestamp
## 1 2015-08-05 19:13:05.000
## 2 2015-08-05 19:27:04.000
## 3 2015-08-05 19:42:57.000
## 4 2015-08-05 19:58:40.000
## 5 2015-08-05 20:15:09.000
## 6 2015-08-05 20:30:28.000
Renames variables to make simpler
colnames(alldat)[3]<-"id"
colnames(alldat)[4]<-"loctime"
Change from factor to character
alldat$loctime<-as.character(alldat$loctime)
Convert datetime into POSIX.ct
alldat$loctime<-as.POSIXct(x = alldat$loctime,format="%Y-%m-%d %H:%M:%S", tz="America/Chicago")
Check for NA's
apply(is.na(alldat), 2, sum)
## location.long location.lat id loctime
## 141 141 0 0
Remove records with NA's
alldat<-na.omit(alldat)
Add Date/Time separate columns
alldat$Hour<-hour(alldat$loctime)
alldat$day<-yday(alldat$loctime)
alldat$Week<-week(alldat$loctime)
alldat$Month<-month(alldat$loctime)
Extract coordinates to convert into a spatial object
coordinates(alldat)<-c("location.long", "location.lat")
Define coordinate reference system
proj4string(alldat)<-CRS("+proj=longlat +datum=WGS84")
Project all coordinates into “USA Contiguous Albers Equal Area Conic” (EPSG:102003)
alldat<-spTransform(alldat, CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5
+lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs "))
Change colname from lat/long to x/y because it's now transformed
colnames(alldat@coords)<-c("x", "y")
Convert back to dataframe from spatial points
alldatdf<-as.data.frame(alldat)
crane.names<-as.list(unique(alldat$id)) #list of cranes to loop through
testdat<-alldatdf
#Create empty objects to define inside loop
temp.crane<-NULL
temp.crane.day<-NULL
dat.summary<-NULL
tempdat<-NULL
firstday<-NULL
lastday<-NULL
dat.summary<-NULL
i<-NULL
j<-NULL
for(i in 1:length(unique(testdat$id))){
temp.crane<-testdat[testdat$id==crane.names[[i]],] # pull off info for each crane
if(nrow(temp.crane)>0){ # this keeps loop from crashing if some cranes don't have data
current.crane<-as.character(crane.names[[i]]) # save name for results dataframe
temp.days<-as.list(unique(temp.crane$day)) # days to loop through for each crane
days.vec<-unlist(temp.days) # convert from a list to a vector
firstday<-days.vec[1] # save the value of the first day to start the sequence
lastday<-last(days.vec) # save the values of the last day to end the sequence
for(j in seq.int(from=firstday, to=lastday)){ # cycle through each day there are locations
tempdat<-temp.crane[which(temp.crane$day==j),] # pull off info for each day
firstrow<-tempdat[1,] # set starting point
lastrow<-tempdat[nrow(tempdat),] # set ending point
daylocs<-rbind(firstrow, lastrow) # combine first and last observations
if((j-1)<firstday) # if there aren't two consecutive days..
{daylocs$night.dist<-NA} # then nightly distance can't be measured
else{prevdat<-temp.crane[which(temp.crane$day==j-1),] #if there are two consecutive days, pull off previous day
firstrow<-prevdat[1,] # take the first location from the previous day
lastrow<-prevdat[nrow(prevdat),] # and the last location
prevdaylocs<-rbind(firstrow, lastrow) # and combine them
daylocs$night.dist<-sqrt((daylocs$x[1]-prevdaylocs$x[2])^2+(daylocs$y[1]-prevdaylocs$y[2])^2) #calculate the distance between the points
}
dat.summary<-rbind(dat.summary, daylocs) # combine results
}}}
dat.summary<-na.omit(dat.summary) # remove rows with NA's
# There are some rows where the first and last locations weren't actually
# at sunrise or sunset, so I'll subset to only keep data in the right time frame
table(dat.summary$Hour)
##
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## 917 13 5 4 42 196 427 217 12 5 6 7 12 8 8 6 9 4
## 18 19 20 21 22 23
## 52 278 328 123 42 883
# I looked up when the possible sunrise/sunset times are for the study area and cut out the times
# that aren't close to the actual sunrise/sunset times.
# I also left in the loc's taken at hour 23 and 0/24, because those are from cranes whose GPS transmitters
# have alternate duty cycles and are actually reflecting roost sites
dat.summary<-dat.summary[which(dat.summary$Hour>=5 &dat.summary$Hour<=7|dat.summary$Hour>=19&
dat.summary$Hour<=21|dat.summary$Hour==23|dat.summary$Hour==0),]
table(dat.summary$Hour)
##
## 0 5 6 7 19 20 21 23
## 917 196 427 217 278 328 123 883
fall.df<-dat.summary
Assign age
fall.df$age<-NULL
adult.list<-c("0J (Waubun adult #2)", "0K Deglman #1","1J (Santiago #3)" ,
"2C (Melby adult)" , "2M (Bagley)" , "3C (Larson adult #2)" ,
"3E (Wambach adult)" , "3K (Rasmussen adult)", "3M (Callaway adult)" ,
"4E Larson adult #1" , "4J (Santiago #1)", "4K (Waubun #1)" , "4M (Ogema adult)" ,
"5J (Deglman #3)" , "5M (Beaulieu adult)" , "6E (Linden-Kologi)", "8K (Deglman #2)" ,
"8M (Stockyard adult)", "9A (Santer)" , "9C (Helliksen adult April 2015)",
"9K (Santiago #2)" ,"9M (Inman adult)" )
fall.df<-mutate(fall.df, age=ifelse(id%in%adult.list, "adult", "colt"))
Assign population
MCP.list<-c("2A (Helliksen July 2015 colt)" ,"2E Beaulieu colt" ,"3E (Wambach adult)",
"4K (Waubun #1)" ,"4M (Ogema adult)" ,"5K (Helliksen July adult 2015)",
"5M (Beaulieu adult)","6E (Linden-Kologi)" ,
"7C (Helliksen colt 2014)" ,"9C (Helliksen adult April 2015)","9J (Waubun colt)")
fall.df$pop<-NULL
fall.df<-mutate(fall.df, pop=ifelse(id%in%MCP.list, "MCP", "EP"))
Assign gender
male.list<-c("3A (Barb Wire)","2E Beaulieu colt" ,"3M (Callaway adult)",
"7M (E of Mud Lake colt)", "7A (Hackensack)", "7C (Helliksen colt 2014)",
"2A (Helliksen July 2015 colt)" ,"9C (Helliksen adult April 2015)",
"5K (Helliksen July adult 2015)", "9M (Inman adult)","1A (Inman colt #2)" ,
"3C (Larson adult #2)" , "7K (Larson colt #1)" , "6E (Linden-Kologi)",
"2C (Melby adult)", "1K (Melby colt #2)","1M (Min/Maint Rd)",
"7E (Nora Township colt)" ,"6A (Pelican Rapids colt)","3K (Rasmussen adult)",
"6C (Rice Lake colt 2014)" , "3E (Wambach adult)" ,"9J (Waubun colt)", "0J (Waubun adult #2)")
fall.df$gender<-NULL
fall.df<-mutate(fall.df, gender=ifelse(id%in%male.list, "male", "female"))
Easy to export if you want
write.csv(x=fall.df, file="fall15.df.csv", row.names=FALSE)
Session Info
devtools::session_info()
## Session info -------------------------------------------------------------
## setting value
## version R version 3.4.0 (2017-04-21)
## system x86_64, mingw32
## ui RStudio (1.0.143)
## language (EN)
## collate English_United States.1252
## tz America/Denver
## date 2017-06-01
## Packages -----------------------------------------------------------------
## package * version date source
## assertthat 0.2.0 2017-04-11 CRAN (R 3.4.0)
## base * 3.4.0 2017-04-21 local
## compiler 3.4.0 2017-04-21 local
## datasets * 3.4.0 2017-04-21 local
## DBI 0.6-1 2017-04-01 CRAN (R 3.4.0)
## devtools * 1.13.1 2017-05-13 CRAN (R 3.4.0)
## digest 0.6.12 2017-01-27 CRAN (R 3.4.0)
## dplyr * 0.5.0 2016-06-24 CRAN (R 3.4.0)
## evaluate 0.10 2016-10-11 CRAN (R 3.4.0)
## ezknitr * 0.6 2016-09-16 CRAN (R 3.4.0)
## gdata * 2.17.0 2015-07-04 CRAN (R 3.4.0)
## graphics * 3.4.0 2017-04-21 local
## grDevices * 3.4.0 2017-04-21 local
## grid 3.4.0 2017-04-21 local
## gtools 3.5.0 2015-05-29 CRAN (R 3.4.0)
## highr 0.6 2016-05-09 CRAN (R 3.4.0)
## hms 0.3 2016-11-22 CRAN (R 3.4.0)
## knitr * 1.15.1 2016-11-22 CRAN (R 3.4.0)
## lattice 0.20-35 2017-03-25 CRAN (R 3.4.0)
## lazyeval 0.2.0 2016-06-12 CRAN (R 3.4.0)
## lubridate * 1.6.0 2016-09-13 CRAN (R 3.4.0)
## magrittr 1.5 2014-11-22 CRAN (R 3.4.0)
## markdown 0.8 2017-04-20 CRAN (R 3.4.0)
## memoise 1.1.0 2017-04-21 CRAN (R 3.4.0)
## methods * 3.4.0 2017-04-21 local
## mime 0.5 2016-07-07 CRAN (R 3.4.0)
## R.methodsS3 1.7.1 2016-02-16 CRAN (R 3.4.0)
## R.oo 1.21.0 2016-11-01 CRAN (R 3.4.0)
## R.utils 2.5.0 2016-11-07 CRAN (R 3.4.0)
## R6 2.2.1 2017-05-10 CRAN (R 3.4.0)
## Rcpp 0.12.10 2017-03-19 CRAN (R 3.4.0)
## readr * 1.1.0 2017-03-22 CRAN (R 3.4.0)
## rgdal * 1.2-7 2017-04-25 CRAN (R 3.4.0)
## rgeos * 0.3-23 2017-04-06 CRAN (R 3.4.0)
## rstudioapi 0.6 2016-06-27 CRAN (R 3.4.0)
## sp * 1.2-4 2016-12-22 CRAN (R 3.4.0)
## stats * 3.4.0 2017-04-21 local
## stringi 1.1.5 2017-04-07 CRAN (R 3.4.0)
## stringr 1.2.0 2017-02-18 CRAN (R 3.4.0)
## tibble 1.3.0 2017-04-01 CRAN (R 3.4.0)
## tools 3.4.0 2017-04-21 local
## utils * 3.4.0 2017-04-21 local
## withr 1.0.2 2016-06-20 CRAN (R 3.4.0)
spun with: ezknitr::ezspin(“Data Processing Scripts/fall.2015.data.processing.R”, out_dir=“output”)