我有一个分类栅格,它有 21 个类别:
>lc_2003
class : SpatRaster
dimensions : 685, 827, 1 (nrow, ncol, nlyr)
resolution : 4477.525, 4477.525 (x, y)
extent : 4224289, 7927202, 2358424, 5425529 (xmin, xmax, ymin, ymax)
coord. ref. : NAD83 / Statistics Canada Lambert
source : memory
name : PHYSIOG
min value : 1
max value : 21
>levels(lc_2003)
[[1]]
[1] ""
我想为每个分类级别命名。但是,当我这样做时,似乎添加了一个额外的级别,因此现在有 22 个级别。列表开头有一个额外的“NA”级别。
landcover_classes_cavmVec <- data.frame(lc_code = 1:21, lc_class = c("Cryptogam, herb barren", "Rush/grass, forb, cryptogam tundra", "Cryptogam barren complex (bedrock)",
"Prostrate dwarf-shrub, herb tundra", "Graminoid, prostrate dwarf-shrub, forb tundra", "Prostrate/Hemiprostrate dwarf-shrub tundra", "Nontussock sedge, dwarf-shrub, moss tundra",
"Tussock-sedge, dwarf-shrub, moss tundra", "Erect dwarf-shrub tundra", "Low-shrub tundra", "Missing (Cryprogram dwarf-shrub?)", "Sedge/grass, moss wetland",
"Sedge, moss, dwarf-shrub wetland", "Sedge, moss, low-shrub wetland", "Noncarbonate mountain complex", "Carbonate mountain complex", "Nunatak complex",
"Glaciers", "Water", "Lagoon", "Non-Arctic areas"))
>levels(lc_2003) <- landcover_classes_cavmVec
> levels(lc_2003)
[[1]]
[1] NA "Cryptogam, herb barren"
[3] "Rush/grass, forb, cryptogam tundra" "Cryptogam barren complex (bedrock)"
[5] "Prostrate dwarf-shrub, herb tundra" "Graminoid, prostrate dwarf-shrub, forb tundra"
[7] "Prostrate/Hemiprostrate dwarf-shrub tundra" "Nontussock sedge, dwarf-shrub, moss tundra"
[9] "Tussock-sedge, dwarf-shrub, moss tundra" "Erect dwarf-shrub tundra"
[11] "Low-shrub tundra" "Missing (Cryprogram dwarf-shrub?)"
[13] "Sedge/grass, moss wetland" "Sedge, moss, dwarf-shrub wetland"
[15] "Sedge, moss, low-shrub wetland" "Noncarbonate mountain complex"
[17] "Carbonate mountain complex" "Nunatak complex"
[19] "Glaciers" "Water"
[21] "Lagoon" "Non-Arctic areas"
我该如何防止这种情况发生?
lc_code
额外的问题:检查变量是否landcover_classes_cavmVec
确实与原始 1:21lc_2003
级别中的等效数字匹配的好策略是什么?我不知道如何验证这段代码是否符合我的要求。