patrick@burns-stat.com
2003-Feb-13 21:57 UTC
[Rd] error from sample if sampling length one character (PR#2546)
Full_Name: Patrick Burns Version: 1.6.1 OS: Suse Linux Submission from: (NULL) (217.36.14.68) Sample gives an error if it is sampling a length one character vector:> sample('a', 1)Error in sample(x, size, replace, prob) : invalid first argument The fix is trivial (numeric test in first line of the body): function (x, size, replace = FALSE, prob = NULL) { if (length(x) == 1 && is.numeric(x) && x >= 1) { if (missing(size)) size <- x .Internal(sample(x, size, replace, prob)) } else { if (missing(size)) size <- length(x) x[.Internal(sample(length(x), size, replace, prob))] } }> sample.fixed('a', 1)[1] "a"
ripley@stats.ox.ac.uk
2003-Feb-23 09:53 UTC
[Rd] Re: error from sample if sampling length one character (PR#2546)
> Sample gives an error if it is sampling a length one character > vector: > > > sample('a', 1) > Error in sample(x, size, replace, prob) : invalid first argument>From the help page:x: Either a (numeric, complex, character or logical) vector of more than one element from which to choose, or a positive integer. so this is as documented, hence not a bug. [Re-created from the R-bugs page as my mail server is offline.] -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
ripley@stats.ox.ac.uk
2003-Feb-23 12:20 UTC
[Rd] Re: error from sample if sampling length one character (PR#2546)
Have you read the R FAQ on what a bug is? It says Finally, a command's intended definition may not be best for statistical analysis. This is a very important sort of problem, but it is also a matter of judgment. Also, it is easy to come to such a conclusion out of ignorance of some of the existing features. It is probably best not to complain about such a problem until you have checked the documentation in the usual ways, feel confident that you understand it, and know for certain that what you want is not available. So, be honest: did you check the documentation? If you did, why did not not mention that you knew it was a documented feature? On Sun, 23 Feb 2003, Patrick Burns wrote:> Just because it is documented does not mean it isn't a bug. > > I found this in a real application that augments a character vector by > randomly > selecting strings from a population. In some circumstances that > population is a > single string. > > I don't see any reason not to make the change.Let us enlighten you: programmers who read the documentation rely on the current behaviour to catch user errors, as they are entitled to do. I trust you have filed a bug report on the `real application' for mis-using R. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595