How to reassign cell/pixel values in R stars objects

Viewed 771

I am new to the stars package in R, and am trying to work out how I assign new values to cells within a two dimensional stars object (a raster in raster package speak). With raster I can do as follows

> library("raster")
> library('stars')
> tif = system.file("tif/L7_ETMs.tif", package = "stars")
> ras<-brick(tif)[[2]] #create raster layer from 2nd layer of multilayer tif
> ras[1,1:5] #see first 5 values of top row of ras
[1] 56 57 52 45 52
> ras[1,1:4] <-100 #replace first 4 values of top row of ras
> ras[1,1:5]
[1] 100 100 100 100  52

According to the st_subset instructions in stars package manual, assignments follow ‘x[i]<-value’ but I am struggling to understand the use of ‘i’ in this context. My failed attempt is below, but I am hoping someone can provide a working alternative.

> tif = system.file("tif/L7_ETMs.tif", package = "stars")
> st<-read_stars(tif)[,,,2] #create raster layer from second layer of multilayer tif
> unlist(st[,1:5,1,]) #see first 5 values of top row of st
L7_ETMs.tif1 L7_ETMs.tif2 L7_ETMs.tif3 L7_ETMs.tif4 L7_ETMs.tif5 
          56           57           52           45           52 
> st[,1:4,1,]<-100 #replace first 4 values of top row of ras
Error in `[<-.stars`(`*tmp*`, , 1:4, 1, , value = 100) : 
  unused arguments (alist(1:4, 1, ))

Many thanks

1 Answers

The stars values can be accessed and modified through the underlying matrix (if single band) or array (if multi-band), accessed with r[[1]].

For example:

library(stars)

# Reading 2nd band
tif = system.file("tif/L7_ETMs.tif", package = "stars")
r = read_stars(tif, RasterIO = list(bands = 2))

# Accessing values (as matrix)
r[[1]][20:140,20:30]

    ##        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
    ##   [1,]   65   66   67   64   61   59   56   39   40    40    40
    ##   [2,]   62   56   52   48   44   42   38   41   40    40    40
    ##   [3,]   50   44   41   40   40   39   40   41   43    42    41
    ##   ...

# Modifying values
r[[1]][20:140,20:30] = 100

# Plot
plot(r)

plot

Related