'How can I prevent an extra level from being added when naming categorical levels in terra SpatRaster?

I have a categorical raster which has 21 categories:

>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] ""

I want to give a name to each of the categorical levels. However, when I do this, an extra level appears to be added, so that now there are 22 levels. There is an extra "NA" level at the beginning of the list.

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" 

How do I prevent this from happening?

Bonus question: what would be a good strategy to check if the lc_code variable in landcover_classes_cavmVec did in fact match up with the equivalent number in the original 1:21 lc_2003 levels? I have no idea how I would verify if this code did what I wanted it to.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source