meine derzeitige if-Abfrage ist sehr lange bzw. wiederholen sich die Abfragen und nur der zugeordnete Werte ändert sich:
Code: Alles auswählen
library(raster)
r_OH <- raster("ohio_area_class.tiff")
h_OH <- raster("ohio_height.tiff")
f2 <- function(x,y){
ifelse( x == 114 & y < 300, 2,
ifelse( x == 114 & y > 300 & y < 599, 1,
ifelse( x == 114 & y > 600 & y < 899, 1,
ifelse( x == 114 & y > 900 & y > 1199, 1,
ifelse( x == 114 & y > 1200, 1,
ifelse( x == 120 & y < 300, 2,
ifelse( x == 120 & y > 300 & y < 599, 1,
ifelse( x == 120 & y > 600 & y < 899, 1,
ifelse( x == 120 & y > 900 & y > 1199, 1,
ifelse( x == 120 & y > 1200, 1,
ifelse( x == 1190 & y < 300, 3,
ifelse( x == 1190 & y > 300 & y < 599, 3,
ifelse( x == 1190 & y > 600 & y < 899, 3,
ifelse( x == 1190 & y > 900 & y > 1199, 1,
ifelse( x == 1190 & y > 1200, 1,
....
ifelse (x == 1200, 0,
ifelse (x == 1210, 0,
ifelse (x == 1220, 0,
ifelse (y > 1500, 0,
NA ))))) ))))) ))))) ... )))) }
rr_neu <- overlay(s_OH, h_OH, fun = f2)
Probiert habe ich folgendes:
Zuerst habe ich die Daten in einen extra data.frame geschrieben und dann versucht mit der sapply-Funktion die vielen if-Abfragen zusammenzufassen.
Code: Alles auswählen
class <- c(114,114,114,114,114,120,120,120,120,120,1190,1190,1190,1190,1190)
kat <- c(2,1,1,1,1,2,1,1,1,1,3,3,3,1,1)
df <- data.frame(class,kat)
str(df)
class kat
1 114 2
2 114 1
3 114 1
4 114 1
5 114 1
6 120 2
7 120 1
8 120 1
9 120 1
10 120 1
11 1190 3
12 1190 3
13 1190 3
14 1190 1
15 1190 1
z <- unique(df$class)
f <- function(kat) {df$kat[sapply(z, function(x) which(x==df$class))]}
[1] 2 1 1 1 1 2 1 1 1 1 3 3 3 1 1
Danke für eure Hilfe
Jessi