Created
May 3, 2016 18:07
-
-
Save revodavid/8c9232e681c5687d5d5b13fddeab9f58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(checkpoint) | |
checkpoint("2016-04-22") | |
library(weatherData) | |
city <- "SJC" | |
cityLongName <- "San Jose" | |
yearStart <- 1991 | |
yearEnd <- 2015 | |
## check for an existing cached data file | |
cacheDir <- "c:/Users/davidsmi/Downloads/WeatherData" | |
filebase <- paste(city, yearStart, yearEnd, sep = "-") | |
filename <- paste(cacheDir, "/", filebase, ".Rd", sep = "") | |
if (file.exists(filename)) { | |
load(filename) | |
} else { | |
## download data from Weather Underground | |
if (!checkDataAvailability(city, paste(yearStart, "-01-01", sep = "")) | |
|| !checkDataAvailability(city, paste(yearEnd, "-12-31", sep = ""))) { | |
stop("Data not available for selected city/dates") | |
} | |
weatherHistory <- NULL | |
for (year in yearStart:yearEnd) { | |
weatherHistory <- rbind(weatherHistory, | |
getWeatherForYear(city, year)) | |
} | |
weatherHistory$Date <- as.Date(weatherHistory$Date) | |
weatherHistory$Year <- as.numeric(format(weatherHistory$Date, "%Y")) | |
weatherHistory$City <- rep(cityLongName, nrow(weatherHistory)) | |
attr(weatherHistory, "CityName") <- city | |
attr(weatherHistory, "CityLongName") <- cityLongName | |
save("weatherHistory", file=filename) | |
} | |
assign(paste(city, "weather", sep = ""), weatherHistory) | |
rm("weatherHistory") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment