2

I am very new to R, so please bear with my explanation. I have a raster list made up of 671 objects that I am hoping to clip to a shapefile.

At this link I explain how I uploaded the original .bin files and got help making a sub-list of just the March and April dates (I'm working with sea ice concentration rasters from NSIDC): Create a new list from a raster list in R

Now, I am hoping to crop that new sub-list. Here is what I have done (where "list_mar_apr" is the sub-list and "boundary" is the shapefile):

# Import and check shapefile
boundary <- readOGR("file_folder/shapefile.shp")
plot(boundary)

# Crop subset to the shapefile
list_mar_apr_crop <- crop(list_mar_apr, boundary)

I keep getting an error message that reads:

unable to find an inherited method for function 'crop' for signature 'list'.

1
  • 1
    Do you want to crop each raster in the list to the shapefile? Does crop(list_mar_apr[[1]], boundary) do the right thing for the first raster? In that case you need to loop over the rasters in the list with lapply. If all the rasters are the same size and extent then alternatively you could make a multi-band stack or brick instead of using a list.
    – Spacedman
    Mar 20, 2020 at 21:46

1 Answer 1

1

Try this:

list_mar_apr_crop <- lapply(X = list_mar_apr, FUN = crop, y = boundary)

This will apply the crop() function to each raster element of your list, using the boundary object for the cropping.

4
  • After trying that, I got this error message: "unable to find an inherited method for function 'crop' for signature 'integer'". I am thinking that it is because the files in the list are originally .bin files, so they appear as just a list of values. Do I need to project them somehow first?
    – Larissa
    Mar 23, 2020 at 18:34
  • Can you post the result of str(list_mar_apr)? Is it occupied by a series of file names, raster objects, or something else entirely? Mar 23, 2020 at 19:05
  • Sorry I don't know how to post a photo, but it says (where ... is a new line): "list of 671... $ 19910301: int 60... $ 19910302: int 61... $ 19910303: int 62"
    – Larissa
    Mar 23, 2020 at 20:15
  • We ended up getting it working! Turns out the last step we did (gis.stackexchange.com/questions/354525/…) was wrong, which was throwing this whole step off. Thanks for your help!
    – Larissa
    Mar 23, 2020 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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