title: Concatenating values into new layer of raster stack
body: <p>I wish to combine the values of two rasters, however by that I mean concatenate instead of sum. For example, instead of 1 + 1 = 2, I want 1 + 1 = 11. Further I want both numbers retained, so 0 + 1 = 01, not 1. I am able to accomplish this by converting the rasters into dataframes, but I can't convert them back to rasters (I'm hoping to make a map as the final product). Further, I can't figure out how to accomplish this while keeping the data a raster class.</p>
  <p>Here is some sample code to illustrate my problem:</p>
  <pre><code>library(raster)
  #Create two rasters
  xmn = 1; xmx = 5; ymn = 10; ymx = 20 
  rast1&lt;- raster(matrix(1,3,4), xmx=xmx, xmn=xmn, ymx=ymx, ymn=ymn)
  rast2&lt;- raster(matrix(2,3,4), xmx=xmx, xmn=xmn, ymx=ymx, ymn=ymn)
  </code></pre>
  <p>All the cells in rast 1 and rast2 are 1 and 2, respectively. Concatenating these two rasters should result in values of 12. However, the only way I know how to do this is converting the rasters into a dataframe, like so:</p>
  <pre><code>#Stack the rasters and convert to data frame
  rast_df&lt;- as.data.frame(stack(rast1, rast2))
  
  #Concatenate using the layer stack (layer.1 for rast1, layer.2 for rast2)
  rast_con&lt;- transform(rast_df, layer.3=paste(layer.1, layer.2, sep = &quot;&quot;))
  
  #Inspect layer.3 values
  head(rast_con)
    layer.1 layer.2 layer.3
  1       1       2      12
  2       1       2      12
  3       1       2      12
  4       1       2      12
  5       1       2      12
  </code></pre>
  <p>As we can see, this approach works. However I can't seem to coerce the dataframe back to the raster with the original extent/CRS with just layer.3. Any tips on how to do this, or how to concatenate the two rasters without converting them to a dataframe, would be helpful.</p>