Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R代码在for循环外按预期工作,但嵌套在for循环/函数中时会得到奇数结果_R_For Loop - Fatal编程技术网

R代码在for循环外按预期工作,但嵌套在for循环/函数中时会得到奇数结果

R代码在for循环外按预期工作,但嵌套在for循环/函数中时会得到奇数结果,r,for-loop,R,For Loop,快速免责声明——我几天前刚拿起R,之前几乎没有编程经验,所以如果这个问题很愚蠢或者代码看起来很糟糕,我向您道歉。(可能是这样) 继续看代码!因此,我正在使用一些我重新调整用途的工具,尝试一些有趣的基于天气的统计建模 我编写的用于下拉天气数据的函数如下所示: getHistoricalWeather <- function(weatherstation, senddate) { base.url <- 'http://api.wunderground.com/api/{MY API

快速免责声明——我几天前刚拿起R,之前几乎没有编程经验,所以如果这个问题很愚蠢或者代码看起来很糟糕,我向您道歉。(可能是这样)

继续看代码!因此,我正在使用一些我重新调整用途的工具,尝试一些有趣的基于天气的统计建模

我编写的用于下拉天气数据的函数如下所示:

getHistoricalWeather <- function(weatherstation, senddate) {
  base.url <- 'http://api.wunderground.com/api/{MY API IS 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)
}
atwaters_cleanup <- function() {
     average_temperature <- 0
     average_hum <- 0
     total_precipi <- 0
     for (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))
}
pull_day <- function() {
          atwaters_historical_data <- data.frame()
     for (i in 1:length(checkdate)) {
          gotweatherdata <- getHistoricalWeather("KBWI",checkdate[i])
          atwaters_historical_data <- rbind(atwaters_historical_data, atwaters_cleanup()) 
     }
     return(atwaters_historical_data)
} 
用于过滤特定信息的函数如下所示:

getHistoricalWeather <- function(weatherstation, senddate) {
  base.url <- 'http://api.wunderground.com/api/{MY API IS 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)
}
atwaters_cleanup <- function() {
     average_temperature <- 0
     average_hum <- 0
     total_precipi <- 0
     for (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))
}
pull_day <- function() {
          atwaters_historical_data <- data.frame()
     for (i in 1:length(checkdate)) {
          gotweatherdata <- getHistoricalWeather("KBWI",checkdate[i])
          atwaters_historical_data <- rbind(atwaters_historical_data, atwaters_cleanup()) 
     }
     return(atwaters_historical_data)
} 
但是,如果我在for循环之外手动尝试相同的命令,它似乎工作正常。在本例中,我只是将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
>获取waters\u历史数据atwaters\u历史数据
X74.2153843846153846 X74.5571428571428 X0.02
1          74.21538          74.55714  0.02
>gotweatherdata atwaters_历史数据atwaters_历史数据
X74.2153843846153846 X74.5571428571428 X0.02
1          74.21538          74.55714  0.02
2          70.96429          69.83333  0.00

我已经尝试过测试for循环,我相当肯定它在checkdate[x](1到10)中插入了正确的数字,但我一直得到相同的重复结果。很抱歉,这看起来一团糟,提前感谢您的帮助

看起来您需要将gotweatherdata输入atwaters_cleanup函数。也就是说,您的函数可以自己工作,但它不会每次都获得新的gotweatherdata。这很有效,谢谢!:DHi欢迎来到。如果您已经解决了问题,我们鼓励您将其作为自己问题的答案提交,然后将其标记为已解决,以便其他搜索类似问题的人可以找到它