Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created October 19, 2012 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/3915776 to your computer and use it in GitHub Desktop.
Save morenoh149/3915776 to your computer and use it in GitHub Desktop.
determine avg hourly temperature of 3 airports via json query
library('RJSONIO')
library('plyr')
# get data
source <- "http://dl.dropbox.com/u/274/readings_formatted.json"
data_raw <- fromJSON(source)
# format data into dataframe
data <- do.call('rbind.fill', lapply(data_raw, as.data.frame))
data$timestamp <- as.POSIXlt(data$timestamp)
data$day <- as.numeric(strftime(data$timestamp, format="%d"))
data$hour <- as.numeric(strftime(data$timestamp, format="%H"))
# subset data by city
sfo <- data[ which(data$location == 'SFO'), ]
nrt <- data[ which(data$location == 'NRT'), ]
iad <- data[ which(data$location == 'IAD'), ]
# deprecated # returns a list of avg temperatures (per hour) given a city
getAverageHourlyTemp <- function(dataset){
v = 1:24
for (i in 1:24){
hourset = dataset[which(dataset$hour == i-1),]
sum = sum(hourset$temperature)
v[i] = sum/nrow(hourset)
}
return(v)
}
# helper function that returns the average temperature of a city given a day and hour
getAvgFromDH <- function(dataset, day, hour){
subset = dataset[which(dataset$hour == hour & dataset$day == day),]
sum = sum(subset$temperature)
return(sum/nrow(subset))
}
# function that proves result is true (verify result)
getX <- function(nrt,iad,sfo){
a = getAvgFromDH(nrt,15,13)
b = getAvgFromDH(iad,16,14)
c = getAvgFromDH(sfo,17,13)
return(floor(c^(b-a-7)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment