I have rasters in memory. For example:
library(raster)
> nclc1B
class : RasterLayer
dimensions : 212, 406, 86072 (nrow, ncol, ncell)
resolution : 2, 2 (x, y)
extent : 662643.7, 663455.7, 3993067, 3993491 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=12 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : in memory
names : band1
values : 3.051758e-05, 21.49905 (min, max)
The rasters are "nclc1B" - "nclc21B". They are adjacent, but don't have same extents. To get all the names so that I could process them, I used
filna <- ls(pattern="nclc*.")
filnb <- gsub("nclc","",filna)
filnc <- as.numeric(gsub("B","",filnb))
filna <- filna[order(filnc)]
> str(filna)
chr [1:21] "nclc1B" "nclc2B" "nclc3B" "nclc4B" "nclc5B" "nclc6B" "nclc7B" "nclc8B" ...
> dput(filna)
c("nclc1B", "nclc2B", "nclc3B", "nclc4B", "nclc5B", "nclc6B",
"nclc7B", "nclc8B", "nclc9B", "nclc10B", "nclc11B", "nclc12B",
"nclc13B", "nclc14B", "nclc15B", "nclc16B", "nclc17B", "nclc18B",
"nclc19B", "nclc20B", "nclc21B")
I thought that with the list of names, I could then use ncell
, to find the number of cells of each respective raster, and a small function I wrote for some simple processing.
> lapply(filna, ncell)
[[1]]
[1] 1
[[2]]
[1] 1
[[3]]
[1] 1
... (the other 18 were all "1" as well)
do.call(ncell, filna)
> do.call(ncell, list(filna))
[1] 21
I am expecting results in the 10,000s for each raster in the list.
> ncell(nclc1B)
[1] 86072
So, I am expecting a list or vector like "86072, 84000, 89000, ..."
My problem seems to be that when I make the list of names, the names get saved as character strings that have no relation to the rasters in memory and the functions are looking for some other object. This looks like something pretty basic, but I have no background in programming and know only the rudiments in R, so there is probably something obvious I am overlooking. This question looked similar to mine, but I am not trying to write files. I've tried
> raster(filna[1])
Error in .local(.Object, ...) :
`c:\Documents\output\nclc1B' does not exist in the file system, and is not recognised as a supported dataset name.
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)
> rasterize(filna[1])
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘rasterize’ for signature ‘"character", "missing"’
I've had this problem before. Generally, I use listoffiles <- ls()
, take listoffiles
and try to run functions on it and nothing happens. How should I proceed with the problem I presented here?