Hot answers tagged

2 votes
Accepted

Dimensions and extent changed after rast from 'xyz' data.frame

as.data.frame(xy=T) will remove cells that are NA. If you have outer rows or columns that are all NA, these will be trimmed off, resulting in a smaller raster on re-conversion. Use na.rm=FALSE in as....
  • 54.1k
2 votes

How can I update a large raster with new values in R with speed?

The one-liner would be: r[] = df$values[match(r[], df$ID)] This uses match to look up the df$values entry for each r[] value matching the df$ID. Note that r[] is pretty much the same as values(r). ...
  • 54.1k
2 votes
Accepted

Raster to SpatialPolygonsDataframe: close/crop polygons at the raster boundary, in R

If you reverse the logic when you set the values of r you can do this. r = raster('./ibcso.tif') r[r < 0] <- NA r[r >= 0] <-1 p = as_Spatial(st_as_sf(stars::st_as_stars(r),as_points = F, ...
  • 54.1k
1 vote
Accepted

What are the xmin, xmax, ymin, ymax values for Australia?

The extent can be extracted from another object. So if you have, e.g. SpatialPolygons for Australia, aus you can do librayr(raster) aus <- raster::getData("GADM", country="Australia&...
1 vote
Accepted

Legend for confusion matrix in R

I suggest using tidyverse and ggplot. Sample code below. With the values parameter in scale_fill_gradientn you can set the values for the individual colors. For other options, I recommend ?...
  • 63
1 vote
Accepted

Reading NetCDF subset R

Allow me to show you how to subset a netcdf file making use of {terra}, replacing the {raster} package (which you referenced in your question), instead of using {ncdf4}: library(terra) #> terra 1.6....
  • 396

Only top scored, non community-wiki answers of a minimum length are eligible