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

时间:2019-04-23 22:06:57

标签: r dataframe lapply

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

我的函数如下:

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)来自Darksky 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 

1 个答案:

答案 0 :(得分:0)

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

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

但是,这可能不是最有效的方法(就计算速度而言)。 因此,如果您的数据集非常大,则可能需要寻找其他解决方案。