'Why do my raster layers' data change when converting from SpatRaster to rasterStack?
I have imported a multi-layer tiff file (7 "bio" layers) using the rast() function from terra and then added other layers to this from elsewhere using:
current.raster <- rast("./bio_layers.tif")
# add1, add2, and add3 are SpatRaster objects from tif files that were modified
# in earlier steps and are stored in memory
new.rast <- c(current.raster, add1, add2, add3)
new.rast
class : SpatRaster
dimensions : 848, 3084, 10 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -141, -115.3, 67.49167, 74.55833 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
sources : bio_layers.tif (7 layers)
memory
memory
... and 1 more source(s)
When I convert this to a rasterStack object using stack(), the values of the "add" layers are changed to become the values of the first layer of current.raster. See summary of my rasters below.
summary(new.rast)
bio1 bio2 bio3 bio4 bio11 bio15 bio18
Min. :-16.60 Min. :1.00 Min. : 12.29 Min. : 0.00 Min. :-32.15 Min. :34.31 Min. : 9.00
1st Qu.:-14.45 1st Qu.:6.95 1st Qu.: 15.29 1st Qu.:14.41 1st Qu.:-30.52 1st Qu.:55.30 1st Qu.: 53.00
Median :-11.74 Median :7.42 Median : 15.90 Median :14.67 Median :-27.87 Median :59.26 Median : 63.00
Mean :-12.07 Mean :7.53 Mean : 16.19 Mean :14.75 Mean :-28.23 Mean :59.21 Mean : 70.24
3rd Qu.: -9.79 3rd Qu.:8.02 3rd Qu.: 16.79 3rd Qu.:15.27 3rd Qu.:-26.35 3rd Qu.:63.18 3rd Qu.: 83.00
Max. : 0.00 Max. :9.56 Max. :100.00 Max. :16.37 Max. : 0.00 Max. :91.12 Max. :153.00
NA's :78844 NA's :78844 NA's :78844 NA's :78844 NA's :78844 NA's :78844 NA's :78844
add1 add2 add3
Min. : -15.42 Min. : 0.00 Min. :0.00
1st Qu.: 38.41 1st Qu.: 0.71 1st Qu.:0.00
Median : 114.79 Median : 1.74 Median :0.00
Mean : 166.82 Mean : 3.45 Mean :0.00
3rd Qu.: 222.14 3rd Qu.: 3.84 3rd Qu.:0.00
Max. :1584.36 Max. :49.38 Max. :0.02
NA's :79057 NA's :79326 NA's :81712
summary(stack(new.rast))
bio1 bio2 bio3 bio4 bio11 bio15 bio18 add1
Min. -16.60000 1.000000e+00 1.253205e+01 0.000000e+00 -32.15000 3.511098e+01 10 -16.60000
1st Qu. -14.45833 6.950000e+00 1.528698e+01 1.441101e+01 -30.51667 5.537055e+01 53 -14.45833
Median -11.77917 7.425000e+00 1.589191e+01 1.467350e+01 -27.88333 5.927956e+01 63 -11.77917
3rd Qu. -9.78750 8.016666e+00 1.679012e+01 1.526218e+01 -26.35000 6.308383e+01 83 -9.78750
Max. 0.00000 9.575000e+00 1.000000e+02 1.638678e+01 0.00000 8.897108e+01 148 0.00000
NA's 2046158.00000 2.046158e+06 2.046158e+06 2.046158e+06 2046158.00000 2.046158e+06 2046158 2046158.00000
add2 add3
Min. -16.60000 -16.60000
1st Qu. -14.45833 -14.45833
Median -11.77917 -11.77917
3rd Qu. -9.78750 -9.78750
Max. 0.00000 0.00000
NA's 2046158.00000 2046158.00000
Notice how the "add" layers data have changed to replicate the "bio1" layer. Why is this happening? Is there a way to preserve the data of these layers stored in memory as I convert it to a rasterStack object?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
