简体   繁体   中英

Problem assigning values to subset (1 row) of terra SpatRaster

I want to replace the values of a given row of a SpatRaster x with the values of a given row of SpatRaster y :

Example data:

library(terra)
r <- rast( extent=c( -108, -105, 39, 42 ), ncol=14, nrow=14, crs="epsg:4326" )
values(r) <- 1:ncell(r)
x <- c(r, r*2, r*3, r*0.5)
x
#class       : SpatRaster 
#dimensions  : 14, 14, 4  (nrow, ncol, nlyr)
#resolution  : 0.2142857, 0.2142857  (x, y)
#extent      : -108, -105, 39, 42  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#sources     : memory  
#              memory  
#              memory  
#              ... and 1 more source(s)
#names       : lyr.1, lyr.1, lyr.1, lyr.1 
#min values  :   1.0,   2.0,   3.0,   0.5 
#max values  :   196,   392,   588,    98 

y <- x
values(y) <- 5

I try to replace 1 row of x with 1 row of y using values()

x[1,,,,drop=FALSE]
#class       : SpatRaster 
#dimensions  : 1, 14, 4  (nrow, ncol, nlyr)
#resolution  : 0.2142857, 0.2142857  (x, y)
#extent      : -108, -105, 41.78571, 42  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : memory 
#names       : lyr.1, lyr.1, lyr.1, lyr.1 
#min values  :   1.0,   2.0,   3.0,   0.5 
#max values  :    14,    28,    42,     7 

y[1,,,,drop=FALSE]
#class       : SpatRaster 
#dimensions  : 1, 14, 4  (nrow, ncol, nlyr)
#resolution  : 0.2142857, 0.2142857  (x, y)
#extent      : -108, -105, 41.78571, 42  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : memory 
#names       : lyr.1, lyr.1, lyr.1, lyr.1 
#min values  :     5,     5,     5,     5 
#max values  :     5,     5,     5,     5 

 values(x[1,,,,drop=FALSE])  |> head(3)
 #     lyr.1 lyr.1 lyr.1 lyr.1
 #[1,]     1     2     3   0.5
 #[2,]     2     4     6   1.0
 #[3,]     3     6     9   1.5

 values(y[1,,,,drop=FALSE]) |> head(3)
      lyr.1 lyr.1 lyr.1 lyr.1
 #[1,]     5     5     5     5
 #[2,]     5     5     5     5
 #[3,]     5     5     5     5

But the following does not work:

> values(x[1,,,,drop=FALSE]) <- values(y[1,,,,drop=FALSE])
Error in .local(x, i, j = j, ..., value) : 
  unused arguments (alist(, drop = FALSE, value))

Why this error? Note that the following works:

x1 <- x[1,,,,drop=FALSE]
y1 <- y[1,,,,drop=FALSE]
values(x1) <- values(y1)

but I need to replace row values within x. Any solution or alternative?

You should not use drop=FALSE in this case, and only one comma.

x[1,] |> head(3)
#  lyr.1 lyr.1.1 lyr.1.2 lyr.1.3
#1     1       2       3     0.5
#2     2       4       6     1.0
#3     3       6       9     1.5

x[1,] <- y[1,]
x[1,] |> head(3)
#  lyr.1 lyr.1.1 lyr.1.2 lyr.1.3
#1     5       5       5       5
#2     5       5       5       5
#3     5       5       5       5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM