2

I have a simple function to create a list of raster files from a list with their names (in .grd format, and called list_names in the example below):

ListRasters <- function(list_names) {

  raster_list <- list() # initialise the list of rasters

  for (i in 1:(length(list_names))){ 
    grd_name <- list_names[i] # list_names contains all the names of the images in .grd format
    raster_file <- raster(grd_name)
  }

  raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
}

# Apply the function 
raster_list <-lapply(list_names, FUN = ListRasters)

The desired result should be in the format:

[[1]]
class : RasterLayer
# etc

[[2]]
class : RasterLayer
# etc

But instead I get them in the format:

[[1]]
[[1]][[1]]
class : RasterLayer 
# etc

[[2]]
[[2]][[1]]
class : RasterLayer
# etc 

That is a problem because later on I can not access the items on the raster list. I can't find a solution because I don't understand why the iteration gives this format of result. Can you give me some explanation or some advice on how to fix the function, or can you see where I am making a mistake?

Any help is more than welcome!

Thankyou!!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3127517
  • 147
  • 1
  • 2
  • 8
  • No need to use `lapply` because that will give you a list of lists (in your case). Use `raster_list <- ListRasters(list_names)` instead. On a sidenote, are you aware that you are appending to the list only after the `for`-loop, but not at each iteration step like described in the code comment? – fotNelton Jan 22 '14 at 11:14
  • What is the output of the `raster` function? – Carlos Cinelli Jan 22 '14 at 11:16

1 Answers1

2

It seems that something is odd with your loop. Nevertheless, the lapply is going to return a list, maybe you should try sapply.

raster_list <-sapply(list_names, FUN = ListRasters)

I think this should give you what you want.

EDIT:

It would be better if you had put a minimal reproducible example, for we do not know how the data is and what is the raster function, but below follows a toy example with the differences of output of lapply and sapply.

PS: and it does seem that your formula iteration is not correct, but you would have to provide more information so we can fix the formula.

list_names <- c("a", "b","c", "d") #defining the list_names

#defining the function
ListRasters <- function(list_names) {

  raster_list <- list() # initialise the list of rasters

  for (i in 1:(length(list_names))){ 
    grd_name <- list_names[i] # list_names contains all the names of the images in .grd format
    raster_file <- paste("file", grd_name, sep=".")
  }

  raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
}

Using lapply:

raster_list <-lapply(list_names, FUN = ListRasters)

The output is:

  [[1]]
[[1]][[1]]
[1] "file.a"


[[2]]
[[2]][[1]]
[1] "file.b"


[[3]]
[[3]][[1]]
[1] "file.c"


[[4]]
[[4]][[1]]
[1] "file.d"

Using sapply:

raster_list <-sapply(list_names, FUN = ListRasters)

The output is:

$a
[1] "file.a"

$b
[1] "file.b"

$c
[1] "file.c"

$d
[1] "file.d"
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
  • I think it doesn't matter whether one uses `lapply` or `sapply`. The result of `ListRasters` is always a list, so either way one will be accumulating lists as the results of iterating over the elements of `list_names`. – fotNelton Jan 22 '14 at 11:56
  • @fotNelton yes, it does matter, 'lapply' will return a list, 'sapply' will simplify the output, I will put a toy example above. – Carlos Cinelli Jan 22 '14 at 12:14
  • 1
    Thanks for the example. What you say is true if the returned lists are always of the same length which may or may not be true for the original question (probably yes, but who knows ;-)). So a counter-example would be `sapply(1:10, function(n) as.list(rep(3,n)))`. – fotNelton Jan 22 '14 at 13:10
  • Yes, you are right. I think it is because in that case `sapply` can't simplify the output. – Carlos Cinelli Jan 22 '14 at 13:21
  • 1
    Thankyou for your help, carloscinelli and fotNelton, i can see now why lapply was not working! – user3127517 Feb 26 '14 at 14:39