0

開始日と終了日を取り、各月の結果のデータ フレームとして返す getWeatherForMonth という関数があります。範囲のデータ フレームを取得する別のメソッド getWeatherForRange があります。「日付」の各行に対して getWeatherForMonth を呼び出し、結果を 1 つのデータ フレームに結合する必要があります。以下のようにmapplyを使用していましたが、結果のデータフレームを結合していません。

library(RJSONIO)

    getWeatherForMonth <- function(start.date, end.date) {
        url <- "http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=PUT-YOUR-KEY-HERE&q=London&format=json&date=%s&enddate=%e&tp=24"
        url <- gsub("%s", start.date, url)
        url <- url <- gsub("%e", end.date, url)

        data <- fromJSON(url)
        weather <- data$data$weather
        GMT <- sapply(weather, function(x){as.character(x[1])})
        Max.TemperatureC <- sapply(weather, function(x){as.numeric(x[3])})
        Min.TemperatureC <- sapply(weather, function(x){as.numeric(x[4])})
        Wind.SpeedKm.h <- sapply(weather, function(x){as.numeric(x$hourly[[1]]$windspeedKmph[1])})
        Precipitationmm <- sapply(weather, function(x){as.numeric(x$hourly[[1]]$precipMM[1])})
        DewPointC <-sapply(weather, function(x){as.numeric(x$hourly[[1]]$DewPointC[1])}) 
        Wind.Chill <-sapply(weather, function(x){as.numeric(x$hourly[[1]]$WindChillC[1])}) 
        Cloud.Cover <-sapply(weather, function(x){as.numeric(x$hourly[[1]]$cloudcover[1])}) 
        Description <-sapply(weather, function(x){as.character(x$hourly[[1]]$weatherDesc[1])}) 
        Humidity <- sapply(weather, function(x){as.numeric(x$hourly[[1]]$humidity[1])}) 
        Feels.LikeC <- sapply(weather, function(x){as.numeric(x$hourly[[1]]$FeelsLikeC[1])})

        df <- data.frame(GMT, Max.TemperatureC, Min.TemperatureC, Wind.SpeedKm.h, Precipitationmm, DewPointC, Wind.Chill, Cloud.Cover, Description, Humidity, Feels.LikeC)

        return(df)
    }

    getWeatherForRange <- function(dates) {
        df <- mapply(getWeatherForMonth, dates$start.date, dates$end.date)

        return(df)
    }

    start.date <- seq(as.Date("2015-01-01"), length=12, by="1 month")
    end.date <- seq(as.Date("2015-02-01"),length=12,by="months") - 1
    dates.2015 <- data.frame(start.date, end.date)

    data <- getWeatherForRange(dates)
    View(data)

出力は次のよう になります 現在の出力のスクリーンショット

4

1 に答える 1

0

の使用を検討してくださいMap()。具体的には、getWeatherForRange関数で、Map()実際には、単純化されていないバージョンの のラッパーである を使用します。これは にmapply()相当しmapply(..., SIMPLIFY=FALSE)ます。デフォルトでmapply()は、ベクトル、行列、またはそれ以上の次元の配列を返します。ただし、データフレーム (つまり、リスト オブジェクト) を返す必要があります。

この更新された関数は、すべての列が各 df で一貫していると仮定して、後で を実行できるデータフレームのリストを返しdo.call(rbind, ...)、最終的なデータフレームのためにすべての dfs を積み重ねます。

getWeatherForRange <- function(dates) {
  # EQUIVALENT LINES
  dfList <- Map(getWeatherForMonth, dates$start.date, dates$end.date)
  # dfList <- mapply(getWeatherForMonth, dates$start.date, dates$end.date, SIMPLIFY = FALSE)
  return(dfList)
}

start.date <- seq(as.Date("2015-01-01"), length=12, by="1 month")
end.date <- seq(as.Date("2015-02-01"), length=12, by="months") - 1
dates <- data.frame(start.date, end.date)

datalist <- getWeatherForRange(dates)         # DATAFRAME LIST
data <- do.call(rbind, datalist)              # FINAL DATA FRAME
于 2016-11-19T21:03:59.067 に答える