Lime
Lime

Reputation: 754

How to extract values across all years?

I am following the code given in: https://github.com/CornellLabofOrnithology/ebird-best-practices/blob/master/03_covariates.Rmd

On arriving at this code:

# extract landcover values within neighborhoods, only needed most recent year
lc_extract_ext <- landcover[[paste0("y", max_lc_year)]] %>% 
                  exact_extract(r_cells, progress = FALSE)
lc_extract_cnt <- map(lc_extract_ext, ~ count(., landcover = value)) %>% 
                  tibble(id = r_cells$id, data = .)
lc_extract_pred <- unnest(lc_extract_cnt, data)

I wished to extract landcover values within neighborhoods across all years.

Instead of using this bit of code max_lc_year, I used this:

# I have thought of using
 all_lc_year <- names(landcover) %>% 
                str_extract("[0-9]{4}") %>% 
                as.integer() 

To extract all years, however, it returns this error at this piece of code:

lc_extract_cnt <- map(lc_extract_ext, ~ count(., landcover = value)) %>% 
                  tibble(id = r_cells$id, data = .)

Error: Problem with mutate() input landcover. x object 'value' not found i Input landcover is value.

I'm thinking I would need to loop, either with map or a for loop, over the years and bind together the resulting data frames.

EDIT: Noticed the previous steps did not work. Here they are, working and corrected!

I have uploaded the landcover .tif files onto my Github here: https://github.com/lime-n/Landcover.git

  1. You can then implement this code to stack them together in R:
library(sf)
library(raster)
library(exactextractr)
library(viridis)
library(tidyverse)
# resolve namespace conflicts
select <- dplyr::select
map <- purrr::map
projection <- raster::projection


landcover <- list.files("insert_folder_name_here", "^modis_mcd12q1_umd", 
                        full.names = TRUE) %>% 
             stack()
# label layers with year
landcover <- names(landcover) %>% 
             str_extract("(?<=modis_mcd12q1_umd_)[0-9]{4}") %>% 
             paste0("y", .) %>% 
             setNames(landcover, .)
landcover
  1. neighborhood_radius <- 5 * ceiling(max(res(landcover))) / 2

  2. I have uploaded the r data at my github here to produce the r_cells data: https://github.com/lime-n/Landcover/blob/master/prediction-surface.tif

and the following code to get the r_cells:

r <- raster("data/prediction-surface.tif")
r_centers <- rasterToPoints(r, spatial = TRUE) %>% 
             st_as_sf() %>% 
             transmute(id = row_number())
r_cells <- st_buffer(r_centers, dist = neighborhood_radius)

I understand the process to download and implement may take a few minutes. However, this code has been bugging me for weeks! any help is appreciated.

Upvotes: 0

Views: 324

Answers (1)

Lime
Lime

Reputation: 754

Found that doing the code individually for each individual year using all_lc_year[1], all_lc_year[2] etc ... and then combining all the rows using rbind(lc_extract_pred, lc_extract_pred_two, lc_extract_pred_three #...etc)

EDIT:

I found that in this code:

lc_extract_cnt <- map(lc_extract_ext, ~ count(., landcover = value)) %>% 
                  tibble(id = r_cells$id, data = .)

The problem has been mentioned similarly here

I tried assigning many different columns under a single column landcover which is why the code never worked. By replacing landcover by the column names needed for the function count, and taking this from

lc_extract_ext <- landcover %>%

instead of the maximum year to include all columns.

Upvotes: 2

Related Questions