[R-sig-Geo] stack() and lapply

Robert J. Hijmans r.hijmans at gmail.com
Wed Nov 10 19:59:09 CET 2010


Hi Roman,

If I understand you well, you have a list of vectors of filenames, and
you would like to make RasterStack objects for each list element.

> library(raster)
> f <- system.file("external/test.grd", package="raster")
> files <- list(c(f,f,f,f), c(f,f,f,f), c(f,f,f,f))
> x = lapply(files, stack)

# I think this works as you can see here:

> x[[1]]
class       : RasterStack
nlayers     : 4
nrow        : 115
ncol        : 80
ncell       : 9200


# What failed was this because it calls the list elements are
apparently displayed using 'print', which fails for a RasterStack,
rather than 'show' which works. Fixed 'print' in next version...
> x
[[1]]
Error in print(<S4 object of class "RasterStack">) :
  no slot of name "file" for this object of class "RasterStack"
>

Robert


On Wed, Nov 10, 2010 at 4:33 AM, Roman Luštrik <roman.lustrik at gmail.com> wrote:
> I have solved the issue by checking that the number of layers is bigger than
> one. It looks to me like stack() has some problems when being fed only one
> layer.
>
> x = character vector
>
>    list.of.files <- lapply(X = list.of.files, FUN = function(x) {
>                if (length(x) > 1) {
>                    c1 <- raster::stack(x)
>                    c1 <- sum(c1, na.rm = TRUE)
>                    c1[c1 == 0] <- NA
>                }
>                if (length(x) == 1) {
>                    c1 <- raster(x)
>                }
>                return(c1)
>            })
>
> Cheers,
> Roman
>
>
>
> On Tue, Nov 9, 2010 at 3:42 PM, Roman Luštrik <roman.lustrik at gmail.com>wrote:
>
>> Dear Listers,
>>
>> I have a list (list.of.files) of file names (character vector) with n
>> number of elements.
>>
>> List of 9
>>  $ 1: chr [1:5] "binned_walker1_1.grd" ...
>>  $ 2: chr [1:2] "binned_walker2_1.grd" ...
>>  $ 3: chr [1:3] "binned_walker3_1.grd" ...
>>  $ 4: chr [1:6] "binned_walker4_1.grd" ...
>>  $ 5: chr "binned_walker5_1.grd" ...
>>  $ 6: chr [1:4] "binned_walker6_1.grd" ...
>>  $ 7: chr [1:6] "binned_walker7_1.grd" ...
>>  $ 8: chr [1:4] "binned_walker8_1.grd" ...
>>  $ 9: chr [1:3] "binned_walker9_1.grd" ...
>>
>> If I call stack() on list elements using, for example
>>
>> stack(list.of.files[[1]])
>>
>> I get a RasterStack object with appropriate number of layers.
>>
>> If I call lapply on list.of.files with
>>
>> lapply(list.of.files, FUN = function(x) stack(x))
>>
>> I get an error
>>
>> $`1`
>> Error in print(<S4 object of class "RasterStack">) :
>>   no slot of name "file" for this object of class "RasterStack"
>>
>> Can anyone elucidate why the method works "by hand" and not using lapply?
>>
>> Cheers,
>> Roman
>>
>>
>>
>> --
>> In God we trust, all others bring data.
>>
>
>
>
> --
> In God we trust, all others bring data.
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>



More information about the R-sig-Geo mailing list