【问题标题】:How to apply function to rows and then bind the results?如何将函数应用于行然后绑定结果?
【发布时间】:2019-04-23 22:06:56
【问题描述】:

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

我的函数如下所示:

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 

【问题讨论】:

  • getForecast 来自哪个包?
  • 它使用了darksky API,我将在函数中添加
  • 它只绑定前两行。 Coordinates 包含 10 行,Lat 和 Lon 名称也被添加到行而不是坐标本身

标签: r dataframe lapply


【解决方案1】:

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

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

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

【讨论】:

  • 当我将其从 2 更改为 5 时收到此错误消息:如果出现错误:需要 TRUE/FALSE 的地方缺少值
猜你喜欢
  • 1970-01-01
  • 2015-02-27
  • 2016-02-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多