I think this answer published by @Zbynek completely response your question:
Reclassifying only part of raster keeping rest unchanged?
library (raster)
# use state bounds from gadm website:
# us = shapefile("USA_adm1.shp")
us <- getData("GADM", country="USA", level=1)
# extract states (need to uppercase everything)
nestates <- c("Maine", "Vermont", "Massachusetts", "New Hampshire" ,"Connecticut",
"Rhode Island","New York","Pennsylvania", "New Jersey",
"Maryland", "Delaware", "Virginia", "West Virginia")
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
# create a random raster over the space:
r = raster(xmn=-85,xmx=-65,ymn=36,ymx=48,nrow=100,ncol=100)
r[]=runif(100*100)
# plot it with the boundaries we want to clip against:
plot(r)
plot(ne,add=TRUE)
# now use the mask function
rr <- mask(r, ne)
# classification of masked cells
rr[] <- ifelse(rr[]>0.5,1,0)
# if you have only one value you want to change, use this:
rr[rr == 1] <- 5
# add classification to the original file - "join" the original, and reclassified raster file
r@data@values[!is.na(rr@data@values)] <- rr@data@values[!is.na(rr@data@values)]
# plot, and overlay:
plot(r);plot(ne,add=TRUE)