0

I am running into a little bit of a problem with a RasterStack loop. I am trying to extract the mean temperature for each county in North Carolina for each hour over the last 10 years. I am able to run some simple code and get this for each layer with relative ease but as I am trying to loop this process to get the mean for every timestamp in the Raster Stack/Brick, I am not having much luck.

library(tidyverse)
library(rgdal)
library(sp)
library(raster)
library(doParallel)
library(foreach)
library(ncdf4)
librarcy(exactextractr)

# Brick the NC File 
r<-stack("ERA_Temps.nc") 

output <- for (i in 1:length(r)) {
                    
              wgsraster <- r[[i]]
                    
              year <- str_sub(names(wgsraster), 2, 5)
              month <- str_sub(names(wgsraster), 7, 8)
              day <- str_sub(names(wgsraster), 10, 11)
              hour <- str_sub(names(wgsraster), 13, 14)
                    
              NC_Counties <- cbind(shp, exact_extract(wgsraster, shp, c('mean')))
                    
              output_df <- data.frame(County.Name = NC_Counties$NAME, GEOID = NC_Counties$GEOID, temp_mean = NC_Counties$mean, year, month, day, hour)
                    return(output_df)
                  }

Output <- output_df

Examples and More Info:

 r
    class      : RasterStack 
    dimensions : 81, 81, 6561, 72  (nrow, ncol, ncell, nlayers)
    resolution : 0.25, 0.25  (x, y)
    extent     : -90.125, -69.875, 24.875, 45.125  (xmin, xmax, ymin, ymax)
    crs        : +proj=longlat +datum=WGS84 +no_defs 
    names      : X2018.09.14.00.00.00, X2018.09.14.01.00.00, X2018.09.14.02.00.00, X2018.09.14.03.00.00, X2018.09.14.04.00.00, X2018.09.14.05.00.00, X2018.09.14.06.00.00, X2018.09.14.07.00.00, X2018.09.14.08.00.00, X2018.09.14.09.00.00, X2018.09.14.10.00.00, X2018.09.14.11.00.00, X2018.09.14.12.00.00, X2018.09.14.13.00.00, X2018.09.14.14.00.00, ... 

I can fairly easily get what I need for an individual layer:

wgsraster <- r[[5]]
                
year <- str_sub(names(wgsraster), 2, 5)
month <- str_sub(names(wgsraster), 7, 8)
day <- str_sub(names(wgsraster), 10, 11)
hour <- str_sub(names(wgsraster), 13, 14)
                
NC_Counties <- cbind(shp, exact_extract(wgsraster, shp, c('mean')))
                
output_df <- data.frame(County.Name = NC_Counties$NAME, GEOID = NC_Counties$GEOID, temp_mean = NC_Counties$exact_extract.wgsraster..shp..c..mean...,
                                        year, month, day, hour)

output_df
     County.Name GEOID temp_mean year month day hour
1         Saluda 45081  298.3141 2018    09  14   11
2   Williamsburg 45089  300.2301 2018    09  14   11
3     Dorchester 45035  299.3716 2018    09  14   11
4       Berkeley 45015  299.7389 2018    09  14   11
5      Greenwood 45047  298.5175 2018    09  14   11

But when I try and run the loop mentioned above, it either fails and gives no output or gives the following error:

Error in .local(x, i, j, ...) : you must provide an index

0

for and return don't work like you think they do in a for loop:

output <- for (i in 1:length(r)) {
                    
      wgsraster <- r[[i]]

      [code]   

      output_df <- data.frame([stuff])
      return(output_df)
}

First, there's no point assigning the value of for to anything:

> z = for(i in 1:10){return(i)}
> z
NULL

its always NULL.

Secondly, return quits the for loop:

> z = for(i in 1:10){message(i);return(i)}
1

here only 1 is printed, and the return is still NULL. Without return:

> z = for(i in 1:10){message(i)}
1
2
3
[up to 10]

What you either want to do is use something like lapply and return a list, or construct a list by extending it within the for loop:

> times2 = lapply(1:3, function(x){c(x,x*2)})
> times2
[[1]]
[1] 1 2

[[2]]
[1] 2 4

[[3]]
[1] 3 6

> times3 = list()
> for(i in 1:3){times3[[i]] = c(i,i*3)}
> times3
[[1]]
[1] 1 3

[[2]]
[1] 2 6

[[3]]
[1] 3 9

It might also be a good practice to put the code you have currently in the loop into a function, so you can test that and then easily put it in an lapply loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.