mutate cannot find function

Viewed 319

I'm working through eBird code from this webpage: https://github.com/CornellLabofOrnithology/ebird-best-practices/blob/master/03_covariates.Rmd

with the exception of using my own data. I have a .gpkg from gadm.org of Australia, and my own ebird data selected for Australia. I have followed out the code exactly with the exception of not using "bcr" as my dataset has no bcr codes, along with removing st_buffer(dist = 10000) from the rgdal code because this prevented me from actually downloading the MODIS data for some reason.

EDIT:I have also used the provided data from the site and still received the same error

I got stuck at this code:

lc_extract <- ebird_buff %>% 
mutate(pland = map2(year_lc, data, calculate_pland, lc = landcover)) %>% 
select(pland) %>% 
unnest(cols = pland)

It returns this error:

Error: Problem with `mutate()` input `pland`.
x error in evaluating the argument 'x' in selecting a method for function 'exact_extract': invalid layer names
i Input `pland` is `map2(year_lc, data, calculate_pland, lc = landcover)`.)`

I can not seem to figure out how to correct it, I'm rather new to dense geo-spatial code like this.

There is a free dataset in the link, but I haven't yet tried it out, so it may be that my data is incompatible with the code? however, I have had a look at the Gis-data.gpkg provided, and my data from gadm seems fine.

The previous two codes to the one above were:

neighborhood_radius <- 5 * ceiling(max(res(landcover))) / 2
 ebird_buff <- red_knot %>% 
     distinct(year = format(observation_date, "%Y"),
              locality_id, latitude, longitude) %>% 
     # for 2019 use 2018 landcover data
     mutate(year_lc = if_else(as.integer(year) > max_lc_year, 
                              as.character(max_lc_year), year),
            year_lc = paste0("y", year_lc)) %>% 
     # convert to spatial features
     st_as_sf(coords = c("longitude", "latitude"), crs = 4326) %>% 
     # transform to modis projection
     st_transform(crs = projection(landcover)) %>% 
     # buffer to create neighborhood around each point
     st_buffer(dist = neighborhood_radius) %>% 
     # nest by year
     nest(data = c(year, locality_id, geometry))
 calculate_pland <- function(yr, regions, lc) {
     locs <- st_set_geometry(regions, NULL)
     exact_extract(lc[[yr]], regions, progress = FALSE) %>% 
         map(~ count(., landcover = value)) %>% 
         tibble(locs, data = .) %>% 
         unnest(data)
 }
1 Answers

This has been answered by the author of the webpage.

The solution was this code:

lc_extract <- NULL
for (yr in names(landcover)) {
  # get the buffered checklists for a given year
  regions <- ebird_buff$data[[which(yr == ebird_buff$year_lc)]]
  # get landcover values within each buffered checklist area
  ee <- exact_extract(landcover[[yr]], regions, progress = FALSE)
  # count the number of each landcover class for each checklist buffer
  ee_count <- map(ee, ~ count(., landcover = value))
  # attach the year and locality id back to the checklists
  ee_summ <- tibble(st_drop_geometry(regions), data = ee_count) %>% 
    unnest(data)
  # bind to results
  lc_extract <- bind_rows(lc_extract, ee_summ)
}

credits go to: Matt Strimas-Mackey

Related