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.lust...@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@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo