r <- raster(nrows=10, ncols=10)
adj <- adjacency(raster=r, fromCells = c(1,30,55,72,100), toCells = c(1:ncell(r)), directions=4)
#put the result in a RasterLayer
v <- vector(length=ncell(r))
v[c(1,30,55,72,100)] <- 1
v[adj[,2]] <- 2
r <- setValues(r, v)
#plot(r)
Run the code above in your browser using DataLab