Check the files you are using! Check that all your raster files have the same CRS and projection. I think the error should be there. I give you one very simple, reproducible and commented example below. Check this code:
# Load libraries
library("raster")
library("mapview") # (not required, but for nice interactive plots)
# Make some example data
# Same extent and projection but different cells values
r1 <- raster(matrix(data = rep(1, 100), nrow = 10, ncol = 10)) # raster1
extent(r1) <- c(-54, -52, -37, -35)
projection(r1) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
r2 <- raster(matrix(data = rep(2, 100), nrow = 10, ncol = 10)) # raster 2
extent(r2) <- c(-54, -52, -37, -35)
projection(r2) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
r3 <- raster(matrix(data = rep(3, 100), nrow = 10, ncol = 10)) # raster 3
extent(r3) <- c(-54, -52, -37, -35)
projection(r3) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# Plot example data
mapview(r1, col.regions = "#EF2929") + mapview(r2, col.regions = "#AD7FA8") + mapview(r3, col.regions = "#729FCF") # stack
# Write rasters to working directory
writeRaster(r1, "r1.tif", overwrite = TRUE)
writeRaster(r2, "r2.tif", overwrite = TRUE)
writeRaster(r3, "r3.tif", overwrite = TRUE)
# List of files
listOfRasterFiles <- list.files(getwd(), pattern = "[r][0-9].tif", full.names = TRUE) # (look for a pattern that have "r", then one number between 0 and 9 and finally ".tif" string)
# One file properties
r1 <- raster("r1.tif")
# class : RasterLayer
# dimensions : 10, 10, 100 (nrow, ncol, ncell)
# resolution : 0.2, 0.2 (x, y)
# extent : -54, -52, -37, -35 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# data source : /home/glopez/r1.tif
# names : r1
# values : 1, 1 (min, max)
# Stack
rasterStack <- stack(listOfRasterFiles)
mapview(stack) # plot stack
# Stack properties
# class : RasterStack
# dimensions : 10, 10, 100, 3 (nrow, ncol, ncell, nlayers)
# resolution : 0.2, 0.2 (x, y)
# extent : -54, -52, -37, -35 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# names : r1, r2, r3
# min values : 1, 2, 3
# max values : 1, 2, 3
# Brick
rasterBrick <- brick(rasterStack)
# class : RasterBrick
# dimensions : 10, 10, 100, 3 (nrow, ncol, ncell, nlayers)
# resolution : 0.2, 0.2 (x, y)
# extent : -54, -52, -37, -35 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# data source : in memory
# names : r1, r2, r3
# min values : 1, 2, 3
# max values : 1, 2, 3
mapview(rasterBrick)
# Mean over layers
mean <- mean(rasterStack)
# class : RasterLayer
# dimensions : 10, 10, 100 (nrow, ncol, ncell)
# resolution : 0.2, 0.2 (x, y)
# extent : -54, -52, -37, -35 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# data source : in memory
# names : layer
# values : 2, 2 (min, max)
Note: The final value of the mean
function is 2 because is the mean of (1 + 2 + 3) / 3. That's because all the rasters have the same repeated value in all the cells: r1 have 100 cells with value 1, r2 have 100 cells with value 2, and so on.
stack(list160927); raster(list160927[1])
and share the output here.