简体   繁体   中英

Translating a `for loop` into purrr using `map` function in r

I need to download weather data from NASA's POWER (Prediction Of Worldwide Energy Resource). The package nasapower is a package developed for data retrieval using R. I need to download many locations (lat, long coordinates). To do this I tried a simple loop with three locations as a reproducible example.

library(nasapower)

data1 <- read.csv(text = "
location,long,lat
loc1, -56.547, -14.2427
loc2, -57.547, -15.2427
loc3, -58.547, -16.2427")

i=1
all.weather <- data.frame()
for (i in seq_along(1:nrow(data1))) {

        weather.data <- get_power(community = "AG",
                          lonlat = c(data1$long[i],data1$lat[i]),
                          dates = c("2015-01-01", "2015-01-10"),
                          temporal_average = "DAILY",
                          pars = c("T2M_MAX"))
        
        all.weather <-rbind(all.weather, weather.data)
}

This works perfect. The problem is that I am trying to mimic this using purrr::map since I want to have an alternative within tidyverse . This is what I did but it does not work:

library(dplyr)
library(purrr)

all.weather <- data1 %>%
    group_by(location) %>%
    map(get_power(community = "AG",
                lonlat = c(long, lat),
                dates = c("2015-01-01", "2015-01-10"),
                temporal_average = "DAILY",
                site_elevation = NULL,
                pars = c("T2M_MAX")))

I got the following error:

Error in isFALSE(length(lonlat != 2)) : object 'long' not found

Any hint on how to run this using purrr ?

To make your code work make use of purrr::pmap instead of map like so:

  1. map is for one argument functions, map2 for two argument funs and pmap is the most general one allowing for funs with more than two arguments.

  2. pmap will loop over the rows of your df. As your df has 3 columns 3 arguments are passed to the function, even if the first argument location is not used. To make this work and to make use of the column names you have to specify the function and the argument names via function(location, long, lat)

library(nasapower)

data1 <- read.csv(text = "
location,long,lat
loc1, -56.547, -14.2427
loc2, -57.547, -15.2427
loc3, -58.547, -16.2427")

library(dplyr)
library(purrr)

all.weather <- data1 %>%
  pmap(function(location, long, lat) get_power(community = "AG",
                                               lonlat = c(long, lat),
                                               dates = c("2015-01-01", "2015-01-10"),
                                               temporal_average = "DAILY",
                                               site_elevation = NULL,
                                               pars = c("T2M_MAX"))) %>% 
  # Name list with locations
  setNames(data1$location) %>% 
  # Add location names as identifiers
  bind_rows(.id = "location")

head(all.weather)
#> NASA/POWER SRB/FLASHFlux/MERRA2/GEOS 5.12.4 (FP-IT) 0.5 x 0.5 Degree Daily Averaged Data  
#>  Dates (month/day/year): 01/01/2015 through 01/10/2015  
#>  Location: Latitude  -14.2427   Longitude -56.547  
#>  Elevation from MERRA-2: Average for 1/2x1/2 degree lat/lon region = 379.25 meters   Site = na  
#>  Climate zone: na (reference Briggs et al: http://www.energycodes.gov)  
#>  Value for missing model data cannot be computed or out of model availability range: NA  
#>  
#>  Parameters: 
#>  T2M_MAX MERRA2 1/2x1/2 Maximum Temperature at 2 Meters (C)  
#>  
#> # A tibble: 6 x 9
#>   location   LON   LAT  YEAR    MM    DD   DOY YYYYMMDD   T2M_MAX
#>   <chr>    <dbl> <dbl> <dbl> <int> <int> <int> <date>       <dbl>
#> 1 loc1     -56.5 -14.2  2015     1     1     1 2015-01-01    29.9
#> 2 loc1     -56.5 -14.2  2015     1     2     2 2015-01-02    30.1
#> 3 loc1     -56.5 -14.2  2015     1     3     3 2015-01-03    27.3
#> 4 loc1     -56.5 -14.2  2015     1     4     4 2015-01-04    28.7
#> 5 loc1     -56.5 -14.2  2015     1     5     5 2015-01-05    30  
#> 6 loc1     -56.5 -14.2  2015     1     6     6 2015-01-06    28.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM