[Rd] A patch for do_sample: check replace arg
Seth Falcon
sfalcon at fhcrc.org
Sat Jan 21 20:20:53 CET 2006
A colleague sent me the following:
If you specify probabilities in the 'sample' function and forget
to type 'prob=...', then you get nonsense. E.g.
sample(1:10,1,c(0,0,0,0,1,0,0,0,0,0))
does not filter '5', while
sample(1:10,1,prob=c(0,0,0,0,1,0,0,0,0,0))
does it correctly. I wish this would return an error because the
'replace' argument should only take logical args. Anyway, it is
easy to make this mistake and having it produce an error would be
nice.
Assuming there is not a use-case for specifying a logical vector for
the 'replace' argument, I like the idea of raising an error if replace
is not length one. The following patch provides an implementation.
+ seth
Diff is against svn Revision: 37141
--- a/src/main/random.c Sat Jan 21 10:54:11 2006 -0800
+++ b/src/main/random.c Sat Jan 21 11:17:20 2006 -0800
@@ -453,15 +453,18 @@
/* with/without replacement according to r. */
SEXP attribute_hidden do_sample(SEXP call, SEXP op, SEXP args, SEXP rho)
{
- SEXP x, y, prob;
+ SEXP x, y, prob, sreplace;
int k, n, replace;
double *p;
checkArity(op, args);
n = asInteger(CAR(args)); args = CDR(args);
k = asInteger(CAR(args)); args = CDR(args);
- replace = asLogical(CAR(args)); args = CDR(args);
+ sreplace = CAR(args); args = CDR(args);
prob = CAR(args);
+ if (length(sreplace) != 1)
+ errorcall(call, _("invalid '%s' argument"), "replace");
+ replace = asLogical(sreplace);
if (replace == NA_LOGICAL)
errorcall(call, _("invalid '%s' argument"), "replace");
if (n == NA_INTEGER || n < 1)
More information about the R-devel
mailing list