1

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?

shea
  • 528
  • 4
  • 17
  • I'm missing something. Why do you have to do so much processing between ls() and lapply()? What happens when you do something simpler. Here's a dumbed down example: `lapply(ls(), function(x) pryr::object_size(x))` – Andrew Brēza Jan 10 '17 at 22:51
  • `"nclc1B"` is just a string, and `filna` is just a character vector. When you `lapply(filna, ncell)`, your are calling `ncell` on strings. You can use `get` (or `mget`) to get the object (list of objects) with a given name, so you could do something like `lapply(filna, function(x) ncell(get(x)))`, or more easily `lapply(mget(filna), ncell)`. Better practice would be to make a list of the rasters rather than giving them iterative variable names. For help with that, see [How do I make a list of data frames?](http://stackoverflow.com/q/17499013/903061). – Gregor Thomas Jan 10 '17 at 22:57
  • @AndrewBrēza, thank you for the suggestion. I lack the experience to see the steps to skip. I thought I would have to build a list and then `lapply` my function to that list. – shea Jan 10 '17 at 23:02
  • @Gregor `get` is what I was looking for. Your suggestion to make a list of rasters is useful, I thought that's what I did with the `ls(pattern=...)`. I am unclear how else to make the list as you suggest, so I will look a the link you provided. As far as my duplicate question, the one you linked to even admits to having an ambiguous title. I tried searching extant questions, but that one never crossed my path. I could argue that question used `list.files()` for external files, while mine uses `ls()` for objects in memory, but they are similar questions. Thanks for the help. – shea Jan 10 '17 at 23:11
  • Regardless of how you got there (`ls` or `list.files`) both you and the other asker ended up with a character vector of object names and needed to use `get`. Same problem, same solution. I also edited the question to make it more searchable - and the duplicate flag will serve as a pointer to make it even more discoverable. Marking as a duplicate is to make the site more navigable and avoid duplication in answer effort - it's nothing against you. – Gregor Thomas Jan 10 '17 at 23:15
  • To make a list of raster objects you should go back to however you created/loaded those objects, and create them in a list instead of individually. The "How do I make a list of data frames?" answer gives a lot of detail. – Gregor Thomas Jan 10 '17 at 23:18
  • 1
    I can't write an answer because the question was marked as a duplicate, but here's my code summarizing what's been said in comments: http://www.r-fiddle.org/#/fiddle?id=oyZDkcBe – Andrew Brēza Jan 11 '17 at 00:34
  • 1
    @AndrewBrēza that looks really useful. Thanks. I wish it was an answer that I could mark as solved. – shea Jan 11 '17 at 14:18

0 Answers0