Skip to content

Instantly share code, notes, and snippets.

@crowding
Last active January 1, 2016 18:48
Show Gist options
  • Save crowding/8185810 to your computer and use it in GitHub Desktop.
Save crowding/8185810 to your computer and use it in GitHub Desktop.
function on characters with recycling
SEXP _find_subst_expressions_list(SEXP strs, SEXP begin, SEXP end) {
int ns, nb, ne;
assert_type(strs, STRSXP);
assert_type(begin, STRSXP);
assert_type(end, STRSXP);
ns = LENGTH(strs);
nb = LENGTH(begin);
ne = LENGTH(end);
int nout;
if (MIN3(ns, nb, ne) == 0) {
nout = 0;
} else {
nout = MAX3(ns, nb, ne);
if ((nout % ns > 0)||(nout % nb > 0)||(nout % ne > 0)) {
warning("longer object length is not a multiple"
" of shorter object length");
}
}
SEXP out = PROTECT(allocVector(VECSXP, nout));
if (ns == nout)
setAttrib(out, R_NamesSymbol, getAttrib(strs, R_NamesSymbol));
for (int i = 0; i < nout; i++) {
SET_VECTOR_ELT(out, i,
find_subst_expressions(STRING_ELT(strs, i%ns),
STRING_ELT(begin, i%nb),
STRING_ELT(end, i%ne)));
}
UNPROTECT(1);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment