如何将函数应用于行然后绑定结果?

问题描述 投票:0回答:1

我有一个包含纬度和经度坐标的数据框。我的函数返回一行数据框,其中包含有关这些坐标的数据。我想对数据框中的所有坐标应用此函数,然后绑定这些结果。

我的函数看起来像这样:

getForecast <- function(lat, lon){
    currentTime = Sys.time();
    gmtTime = as.POSIXct(currentTime)
    gmtTime <- toString(as.POSIXct(gmtTime, "%Y-%m-%dT%H:%M"))
    arr <- unlist(strsplit(gmtTime, ' '))
    curTime <- paste(arr[1], 'T', arr[2], sep="")
    forecast <- get_forecast_for(lat, lon, curTime)
    return(forecast)
  }
getDailyForecast <- function(lat, lon){
    forecast <- getForecast(lat, lon)
    hourly_forecast <- forecast$current
    weather_data <- select(hourly_forecast, 'time', 'temperature', 'humidity', 'windSpeed', 'windBearing', 'cloudCover', 'visibility', 'pressure', 'ozone', 'summary' )
    return(weather_data)
  }

  curForecast <- getDailyForecast(41.870, -87.647)
  curForecast$Lat <- 41.870
  curForecast$Lon <- -87.647
  print(curForecast)

  n_locations <- reactive({
    select(nodeLocations(), Lat, Lon)
  })

getForecast中的get_forecast for(lat,lon,curTime)来自黑暗的天空API

getDailyForecast返回:

time                    temperature humidity windSpeed windBearing cloudCover visibility pressure  ozone       summary     
2019-04-23 16:57:10       51.32     0.67      6.27         103       0.78       8.42      1017.38    331.68   Mostly Cloudy 

在添加纬度和经度后,curForecast看起来像这样:

time temperature humidity windSpeed windBearing cloudCover visibility pressure  ozone       summary      Lon       Lat
1 2019-04-23 16:57:10       51.32     0.67      6.27         103       0.78       8.42  1017.38 331.68 Mostly Cloudy 41.87838 -87.62768

n_locations看起来像这样:

Lat      Lon
-87.62768 41.87838
-87.71299 41.75124
-87.57535 41.72246

我想要一个看起来像这样的数据表:

time                temperature    humidity   windSpeed windBearing cloudCover visibility pressure  ozone       summary      Lon       Lat
2019-04-23 16:57:10       51.32     0.67      6.27         103       0.78       8.42      1017.38   331.68      Mostly Cloudy 41.87838 -87.62768
2019-04-23 16:58:14       55.13      0.6      5.93          91       0.76       9.73      1017.18   329.9       Mostly Cloudy 41.75124 -87.71299
2019-04-23 16:59:13       50.22     0.71      6.33          87       0.87       7.92      1017.4   329.59      Mostly Cloudy 41.72246 -87.57535

编辑:为:

do.call(rbind, apply(coordinates, 2, function(z) getDailyForecast(z[1], z[2]))) 

我得到这个结果:

time temperature humidity windSpeed windBearing cloudCover visibility pressure  ozone       summary
Lat 2019-04-23 00:33:18      -44.83     0.53     21.68         137       0.25       6.91  1024.57 241.12 Partly Cloudy
Lon 2019-04-23 08:33:18       53.69     0.79     10.19         239       0.44       3.90  1028.36 441.36 Partly Cloudy

它只做了前两行坐标

它应该如下所示:

    Lat      Lon         time                temperature    humidity   windSpeed windBearing cloudCover visibility pressure  ozone       summary           
    41.87838 -87.62768    2019-04-23 16:57:10       51.32     0.67      6.27         103       0.78       8.42      1017.38   331.68      Mostly Cloudy 
    41.75124 -87.71299    2019-04-23 16:58:14       55.13      0.6      5.93          91       0.76       9.73      1017.18   329.9       Mostly Cloudy 
    41.72246 -87.57535    2019-04-23 16:59:13       50.22     0.71      6.33          87       0.87       7.92      1017.4   329.59      Mostly Cloudy 
r dataframe lapply
1个回答
0
投票

解决这个问题的经典方法是首先使用apply函数,然后使用do.call将行绑定在一起:

do.call(rbind, apply(mydata, 1, function(z) getDailyForecast(z[1], z[2])))

然而,这可能不是最有效的方式(就计算速度而言)。因此,如果您有一个非常大的数据集,您可能希望寻找不同的解决方案。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.