## PROCESSING DATA # loading packages pcks <- list('lubridate', 'ggmap', 'maptools', 'rgdal', 'maps', 'mapdata', 'move', 'dplyr', 'geosphere') sapply(pcks, library, character = TRUE) # Need to put in username and password below, in quotes login <- movebankLogin(username=xxxx, password=xxxx) caribou <- getMovebankData(study="ABoVE: ADFG Fortymile Caribou (for TWS workshop)", login=login) # If for some reason the line above doesn't work, run this line below: # load("./_data/caribou.raw.rda") # converting to a data frame caribou.df <- as(caribou, "data.frame") str(caribou.df) # renaming columns to make them easier to understand caribou2 <- with(caribou.df, data.frame(id = trackId, lon = location_long, lat = location_lat, time = timestamp, elevation = height_above_ellipsoid)) head(caribou2) # loading in the buffalo data (to demonstrate working with timestamps from .csv file) buffalo <- read.csv("./_data/African_buffalo.csv", sep=",", header=TRUE) str(buffalo) # converting timestamp to POSIXlt format using two different methods head(as.POSIXlt(paste(buffalo$timestamp), format = "%Y-%m-%d %H:%M:%S", tz = "UTC")) head(ymd_hms(buffalo$timestamp)) # the power of the 'lubridate' package! dates <- c("5/7/1976", "10-24:1979", "1.1.2000") mdy(dates) hour(caribou2$time)[1:10] month(caribou2$time)[1:10] yday(caribou2$time)[1:10] # quick plots of the caribou data require(ggplot2) ggplot(caribou2, aes(x = lon, y = lat, col = id)) + geom_path() # determining the correct UTM projection of the caribou data require(PBSmapping) ll <- with(caribou2, data.frame(X = lon, Y = lat)) attributes(ll)$projection <- "LL" xy.km <- convUL(ll, km=TRUE) str(xy.km) # calculating x and y ranges of data diff(range(xy.km$X)) diff(range(xy.km$Y)) # add these transformed coordinates to the caribou data frame caribou3 <- data.frame(caribou2, x=xy.km$X*1000, y=xy.km$Y*1000) # what projection is the 'caribou' object? caribou@proj4string # projecting to utm projection <- CRS("+proj=utm +zone=6 +ellps=WGS84 +datum=WGS84 +units=m +no_defs") caribou.utm <- spTransform(caribou, projection) plot(caribou.utm) # converting to a data frame caribou.utm.df <- as(caribou.utm, "data.frame") names(caribou.utm.df) # adding new columns to data frame for x and y (projected coordinates) xy.projected <- caribou.utm.df[,c("location_long.1", "location_lat.1")] # seeing if the two projections agree diff(range(xy.km[,1])) diff(range(xy.projected[,1]))/1000 diff(range(xy.km[,2])) diff(range(xy.projected[,2]))/1000 # using dplyr's piping to create a summary file require(dplyr) caribou3 %>% group_by(id) %>% summarize( n.obs = length(id), start = as.Date(min(time)), end = as.Date(max(time)), duration = max(time) - min(time), median.Dt = median(diff(time)), range.x = diff(range(x)), range.y = diff(range(y)) ) # two diffeerent plots of temporal extent of data; one for caribou (not interesting), and one for buffalo ggplot(caribou3, aes(x = time, y = id)) + geom_path() ggplot(buffalo, aes(x = ymd_hms(timestamp), y = individual.local.identifier)) + geom_path() # add a column for day.date caribou3$day.date <- as.Date(caribou3$time) # create a leaner summarized file caribou4 <- caribou3 %>% group_by(id, day.date) %>% summarize( time = mean(time), lon = mean(lon), lat = mean(lat), x = mean(x), y = mean(y), elevation = mean(elevation) ) # check out the difference between the old file and summarized file dim(caribou3) dim(caribou4) # "one-step" processing # creating the getUTM function getUTM <- function(data){ ll <- with(data, data.frame(X = lon, Y = lat)) attributes(ll)$projection <- "LL" xy <- convUL(ll, km=TRUE) data.frame(data, x = xy$X, y = xy$Y) } # and doing all the steps above in "one" step! caribou.daily.df <- caribou %>% as("data.frame") %>% dplyr::select(id = trackId, lon = location_long, lat = location_lat, time = timestamp, elevation = height_above_ellipsoid) %>% mutate(day.date = as.Date(time)) %>% getUTM %>% group_by(id, day.date) %>% summarize( time = mean(time), lon = mean(lon), lat = mean(lat), x = mean(x), y = mean(y), elevation = mean(elevation)) %>% as.data.frame() head(caribou.daily.df)