I hope the same question wasn't asked elsewhere, because, even if it is a basic exercise, I couldn't find it in other questions...
I've got a raster, derived from the rasterization of a vector; in this raster, pixels corresponding to the polygons were assigned a number (e.g. all the pixels belonging to polygon A were assigned the number 53; the pixels belonging to polygon H were assigned the number 102). Please notice that the polygons in the original vector do not have an ID code (so, polygon "A" or "H" is something I invented now). This is the structure of the raster I've got:
> structure(lodi_C00)
class : RasterLayer
dimensions : 1994, 1932, 3852408 (nrow, ncol, ncell)
resolution : 30, 30 (x, y)
extent : 516000, 573960, 4990200, 5050020 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Laura\Desktop\MSc thesis\Dati\3_Segmentation\Lodi_segmented\lodi_single_classes_rasterized\lodiC00.tif
names : lodiC00
values : 1, 152 (min, max)
attributes :
ID category
from: 0
to : 152 M158200079
Now I need to select from this raster only the pixels corresponding to some polygons, so pixels with specific values. I've got a list of the pixel values I want to select (there are 106 values):
> C00_trainingrows
[1] 152 62 74 40 102 36 14 78 79 31 35 12 9 137 7 8 43 101 52 133 59 123 23 28 49 93 11 63 72 125 1 69 86
[34] 100 112 145 128 135 32 99 34 44 61 66 47 50 131 129 95 108 76 38 109 39 64 37 53 122 57 21 55 111 113 33 91 77
[67] 132 51 88 10 13 107 24 65 105 60 87 71 147 149 17 139 25 120 124 114 27 45 103 6 84 29 144 141 22 26 5 16 75
[100] 2 41 42 126 118 54 110
So, I want to have a new raster in which only the pixels with the values "152", "62", etc are kept.
I know that when selecting only one value (e.g. 152), this works:
lodi_C00_training <- lodi_C00 == 152
writeRaster(lodi_C00_training, "lodi_C00_training", format="GTiff", progress="text", overwrite=TRUE)
However, I need to have all the 106 values. Any suggestion on how to do it?