Consider the following raster with two categories and a DEM (numeric raster), I'd like to compute the elevation difference along the border (closest pixel) of those two categories, it doesn't matter on which side of the border the result is stored. I've checked r.neighbors but it's not category-aware. It's of little matter what it does in corners, where there are two closest pixels.
You could make a new raster that is only the red category using
r.mapcalc. Grow it by one cell using
r.grow. Fill the growth region using one of several algorithm (
r.mfilter?). There is now a one cell border overlap between the new raster and the pink cells in the original. Subtract the two using
I think the above steps should get you what you want. There may be more efficient ways, or even a single command that does what you want. I'd ask on the GRASS list which seems to have more eyeballs than the GRASS topic on this site.
To get the inner and outer border of a raster polygon map, you can use r.neighbors along with the range method:
# North Carolina sample dataset g.region raster=geology_30m -p r.neighbors input=geology_30m output=geology_30m_boundary method=range
This you could then use as a mask and continue with the further statistical analysis.