0

I'm trying to stack 21 raster files using this code:

setwd("D:/LANDSAT8/2016/LC8221077/all/")

img = stack(list.files(pattern=".TIF$"))

names(img) <- c(paste0("B", 1:21, coll = "")) 

But when I run it, it show's this error message:

Error in rep.int(names(x), lengths(x)) : invalid 'times' value

How can I solve it?

loki
  • 9,816
  • 7
  • 56
  • 82
  • can you please edit the console output of `img` into your question? – loki Jan 06 '17 at 17:42
  • The `names...` line should work fine if you have the correct number of layers in the stack. 1st check how many layers. Or if error is in the `img=stack...` line? If so, what does `list.files(pattern=".TIF$")` by itself produce? – dww Jan 06 '17 at 17:46
  • 3
    Also, have a thourough read of [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), especially the section on rasters. You need to make a reproducible example before posting here. As it stands no-one but you can run your code to test it. There is a good chance that during the creation of a minimal reproducible example you will find out for yourself what the issue is. – dww Jan 06 '17 at 17:54
  • 1
    wild guess: have you loaded the raster library: `library(raster)` . I got this error message once when I forgot to load it in my script. – maRtin Jan 06 '17 at 19:35
  • @loki the initial post didn't have the `library(raster)` call. Did you add it in your edit? – maRtin Jan 07 '17 at 10:30
  • 1
    @maRtin, that is correct. I did not think of the possibility that it was missing. – loki Jan 07 '17 at 11:21

1 Answers1

0

as @maRtin explained in his comment: you probably forgot to load

library(raster)

Thus, you use utils::stack.default() which is not defined for Raster*Objects as also mentioned here.

loki
  • 9,816
  • 7
  • 56
  • 82