0

I have 11 raster files (waveheightfiles), and need to extract one value from a different set of coordinates (station) for each raster. I have a list of 11 coordinates (df2) and have used part of the raster file name as an ID column. Can I create a loop or function where I extract the correct station value from each individual raster, and store in a matrix or dataframe?

I have attempted to create a loop below but not sure it is correct:

   id        lat      lon

1  511.1100 -4.087830 53.55292

2  511.1300 -4.106083 53.55458

3  511.1500 -4.075414 53.55917

4  511.1700 -4.105833 53.56083

5   511.900 -4.098334 53.55375

6  611.1000 -4.126676 53.53500

etc...

waveheightfiles [1] "waveheight_511.1100.tif" "waveheight_511.1300.tif" "waveheight_511.1500.tif" [4] "waveheight_511.1700.tif" "waveheight_511.900.tif" "waveheight_611.1000.tif" [7] "waveheight_611.1200.tif" "waveheight_611.1400.tif" "waveheight_611.1600.tif" [10] "waveheight_711.1100.tif" "waveheight_711.900.tif"

d <- 1:11

for (i in 1:length(d)) {

  imported_raster <- raster(waveheightfiles[i])
  lon_sta <- df2$lon[i] # poi lon
  lat_sta <- df2$lat[i] # poi lat
  xy <- cbind(lon_sta, lat_sta)
  v <- extract(imported_raster, xy)  
}

Your Answer

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

Browse other questions tagged or ask your own question.