[R-sig-Geo] subsetting terra::raster

Michael Sumner md@umner @end|ng |rom gm@||@com
Tue Jul 14 06:40:47 CEST 2020


The second argument to xyFromCell is a *cell number*, but in `r[r>0]` you
have a *cell value*, which may not be what you want.

It's the r[r > 0] part that's not working in terra, so you could do this as
a workaround ...

xy <- xyFromCell(r, values(r)[values(r) >0])

(but are your cell values really intended for use as cell numbers? It's
certainly useful for that but probably uncommon).

Your code also did not set the extent in the terra object, or set.seed so
there's several steps that lack compatibility.

Cheers, Mike



On Tue, Jul 14, 2020 at 2:18 PM Herr, Alexander (L&W, Black Mountain)
<Alexander.Herr using csiro.au> wrote:

> Hi,
>
> I am moving from raster to terra. I get an error when I adapt the
> following raster code:
> require(raster)
> set.seed(666)
>  reps=10
>  r <- raster( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
>  extent(r)<- c(1,101,1,101)
> plot(r)
> xy<-xyFromCell(r,r[r>0])
> z<-r[r>0]
> cbind(xy,z)
>
> Under terra code:
> require(terra)
> r <- rast( matrix(nrow=reps,ncol=reps, round(rnorm(reps^2),1)))
> plot(r)
> xy<-xyFromCell(r,r[r>0])
> Error in r[r > 0] : object of type 'S4' is not subsettable
>
> What am I doing wrong?
>
> Thanks
> herry
>
>
>  in terra
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner using gmail.com

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list