Site icon R-bloggers

R and the Weather

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

by Joseph Rickert

The weather is on everybody's mind these days: too much ice and snow east of the Rockies and no rain to speak fo in California. Ram Narasimhan has made it a little easier for R users to keep track of what's going on and also get a historical perspective. His new R package weatherData makes it easy to down load weather data from various stations around the world collecting data.  Here is a time series plot of the average temperature recorded at SFO last year with the help of the weatherData's getWeatherForYear() function. It is really nice that the function returns a data frame of hourly data with the Time variable as class POSIXct.

### Some Code to Get Average Temperature at SFO
# JB Rickert
library(weatherData)
library(ggplot2)
library(scales)
library(plyr)
 
w2013 <- getWeatherForYear("sfo",2013)
 
w2013$shortdate <- strftime(w2013$Time, format="%m-%d")
 
meanTemp <- ddply(w2013, .(shortdate), summarize, mean_T=mean(TemperatureF))
meanTemp$shortdate <- as.Date(meanTemp$shortdate,format="%m-%d")
 
ggplot(meanTemp, aes(shortdate, mean_T)) + geom_line() +
  scale_x_date(labels=date_format("%m/%d")) + xlab("") + ylab("Mean Temp deg F") +
  ggtitle("2013 Average Daily Temperature at SFO")

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.