I see, the selection of the layers in shape or raster file is tricky.

Here what I found out:
- It seems that my raster file r is fine, but it is definitively my shape file 
s that causes problems reading the right layer: read_sf should be ok, because 
it reads in all layers, names(sf). However, when selecting the third layer it 
reads in number 3 (Unterregio) and number 15 (geometry). Secondly when using 
stack() it does not read in the s layer (probably because there are two layers).

> 
> sf <- read_sf("C:/Users/Sibylle 
> Stöckli/Desktop/NCCS_Impacts_Lot2_2022/InVEST/BAFU_ALLEMA_strata/Grossraume/data/BiogeographischeRegionen/N2020_Revision_BiogeoRegion.shp")
> names(sf)
 [1] "RegionNumm" "RegionName" "Unterregio" "Unterreg_1" "ObjNummer"  "Version" 
   "Shape_Leng" "Shape_Area" "DERegionNa" "FRRegionNa" "ITRegionNa" 
"DEBioBedeu" "FRBioBedeu" "ITBioBedeu"
[15] "geometry"  
> 
> s<-sf[3]
> names(s)
[1] "Unterregio" "geometry"  
>

> r<-allrasters_pres[[1]]
> names(r)
[1] "Andrena.barbilabris_glo_ensemble"
> 
> 
> rs <- stack(r, s)
> names(rs) <- c('r', 's')
Error in `names<-`(`*tmp*`, value = c("r", "s")) : 
  incorrect number of layer names
> names(rs)
[1] "Andrena.barbilabris_glo_ensemble"
> 
>



-----Original Message-----
From: Ivan Krylov <ikry...@disroot.org> 
Sent: Saturday, August 24, 2024 6:04 PM
To: sibylle.stoec...@gmx.ch
Cc: 'SIBYLLE STÖCKLI via R-help' <r-help@r-project.org>
Subject: Re: [R] paired raster boxplots

В Sat, 24 Aug 2024 10:24:36 +0200
<sibylle.stoec...@gmx.ch> пишет:

> Yes indeed my raster "s" (the shape file for the boxplot classes) has 
> several layers.

If 's' contains more than one layer, then this already prevents you from giving 
two names to stack(r, s).

> That's way I tried to select a layer by "
> s<-sf$Unterregio".

'sf' is a data.frame-like object returned by read_sf, not a raster. If 's' is a 
raster, it could still contain multiple layers.

> > sf <- read_sf("C:/Users/....._BiogeoRegion.shp")
> > names(sf)
> 
> > names(sf)
>  [1] "RegionNumm" "RegionName" "Unterregio" "Unterreg_1" "ObjNummer"
> "Version"    "Shape_Leng" "Shape_Area" "DERegionNa" "FRRegionNa"
> "ITRegionNa" "DEBioBedeu" "FRBioBedeu" "ITBioBedeu"
>  [15] "geometry"
> 
> > s<-sf$Unterregio
> > r<-allrasters_pres[[1]]

Sorry, that's still not enough information because we don't know what
names(rs) is. Since 'allrasters_pres' is a list of rasters, 'r' could also 
contain more than one layer, resulting in stack(r, s) containing more than two 
layers. 

In order to avoid the error, you need to see names(rs) and either give the same 
number of names to the object instead of two, or additionally extract one layer 
(using raster(r, layer = NUMBER_OR_NAME)) from each of 'r' and 's' before 
stacking them.

--
Best regards,
Ivan

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to