R code outside of for-loop working as intended but when nested in for-loop/function getting odd results -


quick disclaimer-- picked r few days ago no prior programming experience apologize if question silly or code looks awful. (it is).

on code! i'm using code blog repurposed try fun statistical modeling based on weather.

the function have written pull down weather data looks so:

gethistoricalweather <- function(weatherstation, senddate) {   base.url <- 'http://api.wunderground.com/api/{my api here}/'   final.url <- paste(base.url, 'history_', senddate, '/q/', weatherstation, '.json',sep='')   conn <- url(final.url)    raw.data <- readlines(conn, n=-1l, ok=true)    weather.data <- fromjson(paste(raw.data, collapse=""))    close(conn)   return(weather.data) } 

after that, i'm trying loop through 10 separate dates , pull down weather data each of dates, find average temperature based between hours, average humidity, , total rainfall. dates saved in variable called checkdate.

checkdate  [1] "20130628" "20130611" "20130612" "20130613" "20130614" "20130615" "20130616" "20130617" "20130618" "20130619" 

and function filter out specific information looks so:

atwaters_cleanup <- function() {      average_temperature <- 0      average_hum <- 0      total_precipi <- 0      (i in seq_along(gotweatherdata$history$observations)) {          if (as.numeric(gotweatherdata$history$observations[[i]]$date$hour) >= 6 && as.numeric(gotweatherdata$history$observations[[i]]$date$hour) < 18) {                             average_temperature <- c(average_temperature,as.numeric(gotweatherdata$history$observations[[i]]$tempi))            average_hum <- c(average_temperature,as.numeric(gotweatherdata$history$observations[[i]]$hum))            if (as.numeric(gotweatherdata$history$observations[[i]]$precipi) > 0) {total_precipi <- c(total_precipi,as.numeric(gotweatherdata$history$observations[[i]]$precipi))}          }     }     average_temperature <- sum(average_temperature) / length(average_temperature)     average_hum <- sum(average_hum) / length(average_hum)     total_precipi <- sum(total_precipi)     return(c(average_temperature, average_hum, total_precipi)) } 

and for-loop in question looks so:

pull_day <- function() {           atwaters_historical_data <- data.frame()      (i in 1:length(checkdate)) {           gotweatherdata <- gethistoricalweather("kbwi",checkdate[i])           atwaters_historical_data <- rbind(atwaters_historical_data, atwaters_cleanup())       }      return(atwaters_historical_data) }  

the problem i'm running for-loop seems return 10 instances of same day:

   x70.9642857142857 x69.8333333333333 x0 1           70.96429          69.83333  0 2           70.96429          69.83333  0 3           70.96429          69.83333  0 4           70.96429          69.83333  0 5           70.96429          69.83333  0 6           70.96429          69.83333  0 7           70.96429          69.83333  0 8           70.96429          69.83333  0 9           70.96429          69.83333  0 10          70.96429          69.83333  0 

but if manually try same commands outside of for-loop seems working properly. in example i'm changing checkdate[1] checkdate[2]

> gotweatherdata <- gethistoricalweather("kbwi",checkdate[1]) > atwaters_historical_data <- rbind(atwaters_historical_data,atwaters_cleanup()) > atwaters_historical_data    x74.2153846153846 x74.5571428571428 x0.02 1          74.21538          74.55714  0.02  > gotweatherdata <- gethistoricalweather("kbwi",checkdate[2]) > atwaters_historical_data <- rbind(atwaters_historical_data,atwaters_cleanup()) > atwaters_historical_data    x74.2153846153846 x74.5571428571428 x0.02 1          74.21538          74.55714  0.02 2          70.96429          69.83333  0.00 

i've tried testing for-loop , i'm it's plugging correct numbers checkdate[x] (1 10) keep getting same repeated results. sorry looks mess , in advance help!


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -