#load packages
library(weatherData) #pull weather data from wundeground
library(dplyr) #manipulate data frames
library(lubridate) #maniuplate dates and times
library(birk) #for the which.closest() function
library(rmarkdown)
#load telemetry encounter data, the "\t" is because my CSV is tab delimited
claireDT <- read.csv("C:/Users/John/Dropbox/Grad School/Manuscripts/Vanek and Burke 2017/R code/claire 2014 date time.csv",header=T, na.strings=c("NA","na","", " "),"\t")
#turn dates into the date class
claireDT$Date <- as.Date(claireDT$Date,format="%d-%b-%y")
#tell R what format my times are in
claireDT$Time <- hm(claireDT$Time)
#create a joined date time column. ymd_hms is the year month date hour minute second format, paste combines the two columns
claireDT$DateTime <- ymd_hms(paste(claireDT$Date,claireDT$Time))
#turn time into a decimal so that I can compare it later
claireDT$decTime <- hour(claireDT$Time) * 60 + minute(claireDT$Time) + second(claireDT$Time) / 60
# "for loops" in R are picky, so it was recomended to clear out potential new dataframes
date1 <- NULL
temp <- NULL
#determine the number of rows in telemetry data, used below in loop
x<- as.numeric(count(claireDT))
#starts loop to get temperature data for each time the snake was found
for (i in 1:x){
#prints the loop iteration so I can check on the progress
cat("run # ", i)
#pull temperature from wunderground and store it in a dataframe, tryCatch, error, and NA tell the loop to ignore any NAs in the data, otherwise the loop will crash
date1 <- tryCatch(getWeatherForDate("KJFK",claireDT$Date[i],opt_detailed=T),error=function (err) NA)
#create those decimal times again for this data frame
date1$dec<-tryCatch(hour(date1$Time) * 60 + minute(date1$Time) + second(date1$Time) / 60,error=function(err) NA)
#find closest time temperature data was taken and compare to my snake data
y<- tryCatch(which.closest(date1$dec,claireDT$decTime[i]),error=function(err) NA)
#add the temperature data to the initial telemetry dataframe
tryCatch(claireDT[i,8] <- date1$Temperature[y],error=function(err) NA)
#ends loop
}
#rename column
names(claireDT)[names(claireDT) == "V8"] <- "tempF"
#plot new temp data
plot(claireDT$temp)
library(weatherData) #pull weather data from wundeground
library(dplyr) #manipulate data frames
library(lubridate) #maniuplate dates and times
library(birk) #for the which.closest() function
library(rmarkdown)
#load telemetry encounter data, the "\t" is because my CSV is tab delimited
claireDT <- read.csv("C:/Users/John/Dropbox/Grad School/Manuscripts/Vanek and Burke 2017/R code/claire 2014 date time.csv",header=T, na.strings=c("NA","na","", " "),"\t")
#turn dates into the date class
claireDT$Date <- as.Date(claireDT$Date,format="%d-%b-%y")
#tell R what format my times are in
claireDT$Time <- hm(claireDT$Time)
#create a joined date time column. ymd_hms is the year month date hour minute second format, paste combines the two columns
claireDT$DateTime <- ymd_hms(paste(claireDT$Date,claireDT$Time))
#turn time into a decimal so that I can compare it later
claireDT$decTime <- hour(claireDT$Time) * 60 + minute(claireDT$Time) + second(claireDT$Time) / 60
# "for loops" in R are picky, so it was recomended to clear out potential new dataframes
date1 <- NULL
temp <- NULL
#determine the number of rows in telemetry data, used below in loop
x<- as.numeric(count(claireDT))
#starts loop to get temperature data for each time the snake was found
for (i in 1:x){
#prints the loop iteration so I can check on the progress
cat("run # ", i)
#pull temperature from wunderground and store it in a dataframe, tryCatch, error, and NA tell the loop to ignore any NAs in the data, otherwise the loop will crash
date1 <- tryCatch(getWeatherForDate("KJFK",claireDT$Date[i],opt_detailed=T),error=function (err) NA)
#create those decimal times again for this data frame
date1$dec<-tryCatch(hour(date1$Time) * 60 + minute(date1$Time) + second(date1$Time) / 60,error=function(err) NA)
#find closest time temperature data was taken and compare to my snake data
y<- tryCatch(which.closest(date1$dec,claireDT$decTime[i]),error=function(err) NA)
#add the temperature data to the initial telemetry dataframe
tryCatch(claireDT[i,8] <- date1$Temperature[y],error=function(err) NA)
#ends loop
}
#rename column
names(claireDT)[names(claireDT) == "V8"] <- "tempF"
#plot new temp data
plot(claireDT$temp)