This is a C implementation
library(inline)
subraw <- cfunction(c(x="raw", i="integer", j="integer"), "
int n = INTEGER(j)[0] - INTEGER(i)[0] + 1;
SEXP result;
if (n < 0)
Rf_error(\"j < i - 1\");
result = Rf_allocVector(RAWSXP, n);
memcpy(RAW(result), RAW(x) + INTEGER(i)[0] - 1L, n);
return result;
")
with the usual caveats about missing sanity checks (e.g., i, j scalar and not NA, i > 0; j <= length(x), etc.). In action
> xx = readBin("~/bin/R-devel/lib/libR.so", raw(), 6000000)
> length(xx)
[1] 5706046
> length(subraw(xx, 1L, length(xx)))
[1] 5706046
> system.time(subraw(xx, 1L, length(xx)))
user system elapsed
0.000 0.000 0.001
subraw(xx, 10L, 9L)
returns raw(0)
.