ich habe ein Rasterlayer welches verschiedene Länderklassen enthält und wie folgt aufgebaut ist:
Code: Alles auswählen
r
class : RasterLayer
dimensions : 100, 100, 10000 (nrow, ncol, ncell)
resolution : 0.07629955, 0.02648415 (x, y)
extent : 19.530786, 27.16074, 36.3723, 59.02071 (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 : 1, 27 (min, max)
attributes :
ID key gebiet name
from: 1 25DB 1.1 Virgina - LampStreet
to : 27 E3 8.1 Westvirginia - Tiefland
Jetzt möchte ich alle Unterpunkte mit 1 zu 1 zusammenfassen, 2 zu 2 und so weiter, dass am Ende nur noch neun Gebiete übrig bleiben anstatt der 27.
Probiert habe ich das mal so:
Code: Alles auswählen
library(raster)
rat <- levels(r)[[1]]
rat$land <- c(1:27)
rat
ID key gebiet name land
0 1 25DB 1.1 Virgina - LampStreet 1
1 2 25DF 1.2 Virgina - University 2
2 3 25E7 4.1 DC - Capitol 3
3 4 25E8 2.2 Maryland - Mainstreet 4
...
r[r %in% c(1.1:1.4)] <- 1
r[r %in% c(2.1:2.4)] <- 2
r[r %in% c(3.1:3.4)] <- 3
r[r %in% c(4.1:4.4)] <- 4
r[r%in% c(5.1:5.4)] <- 5
r[r %in% c(6.1:6.4)] <- 6
r[r %in% c(7.1:7.4)] <- 7
r[r %in% c(8.1:8.4)] <- 8
r[r %in% c(9.1:9.4)] <- 9
r
class : RasterLayer
dimensions : 100, 100, 10000 (nrow, ncol, ncell)
resolution : 0.07629955, 0.02648415 (x, y)
extent : 19.530786, 27.16074, 36.3723, 59.02071 (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 : 1, 27 (min, max)
attributes :
ID key gebiet name
from: 1 25DB 1.1 Virgina - LampStreet
to : 27 E3 8.1 Westvirginia - Tiefland
r <- raster::ratify(r)
rat <- data.frame(ID = 1:9, land = c("a","b","c","d","e","f","g","h","j"))
levels(r) <- rat
Warnmeldungen:
1: In .checkLevels(levels(x)[[1]], value) :
the number of rows in the raster attributes (factors) data.frame is unexpected
2: In .checkLevels(levels(x)[[1]], value) :
the values in the "ID" column in the raster attributes (factors) data.frame have changed
r
class : RasterLayer
dimensions : 100, 100, 10000 (nrow, ncol, ncell)
resolution : 0.07629955, 0.02648415 (x, y)
extent : 19.530786, 27.16074, 36.3723, 59.02071 (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 : 1, 27 (min, max)
attributes :
ID land
from: 1 a
to : 9 j
Code: Alles auswählen
v <- values(r)
table(v)
Habe ich hier irgendwo einen Denkfehler, oder stimmt der Ansatz zumindest und ich habe einen Fehler beim ausprogrammieren gemacht?
Wäre toll, wenn mal ein geschultes Auge von euch drüber schauen könnte.
Danke vielmals.
Jessi