Travis McArthur
2016-Jul-16 19:40 UTC
[Rd] sample() fails with double or integer NA input of length one
Hi, I have discovered that sample() fails with an uninformative error message when the x argument is a single NA of type double or integer. I can reproduce the problem with the following code: base::sample(NA) # NA is of logical type above base::sample(NA_character_) base::sample(NA_complex_) base::sample(NA_real_) base::sample(NA_integer_) The last two lines throw the following error: Error in if (length(x) == 1L && is.numeric(x) && x >= 1) { : missing value where TRUE/FALSE needed My sessionInfo() : R version 3.3.1 (2016-06-21) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.11.5 (El Capitan) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base Best, Travis McArthur
Martin Maechler
2016-Jul-18 08:41 UTC
[Rd] sample() fails with double or integer NA input of length one
>>>>> Travis McArthur <travis.d.mcarthur at gmail.com> >>>>> on Sat, 16 Jul 2016 14:40:28 -0500 writes:> Hi, > I have discovered that sample() fails with an uninformative error > message when the x argument is a single NA of type double or integer. > I can reproduce the problem with the following code: > base::sample(NA) > # NA is of logical type above > base::sample(NA_character_) > base::sample(NA_complex_) > base::sample(NA_real_) > base::sample(NA_integer_) > The last two lines throw the following error: > Error in if (length(x) == 1L && is.numeric(x) && x >= 1) { : > missing value where TRUE/FALSE needed Good catch, thank you, Travis! The error message --- not quite uninformative I would argue --- actually even suggests a fix -- which I have already committed to the R sources: Replace is.numeric() by is.finite(). However, that is not good enough: With that change, sample(1i) fails. So, in a better patch, the line in case if (length(x) == 1L && is.numeric(x) && x >= 1) { could be replaced by if (length(x) == 1L && is.numeric(x) && is.finite(x) && x >= 1) { or if (length(x) == 1L && is.numeric(x) && !is.na(x) && x >= 1) { or does anyone see a nicer / more performant efficient solution? Martin [............] > Best, > Travis McArthur
Possibly Parallel Threads
- c(NA, 0+1i) not the same as c(as.complex(NA), 0+1i)?
- c(NA, 0+1i) not the same as c(as.complex(NA), 0+1i)?
- c(NA, 0+1i) not the same as c(as.complex(NA), 0+1i)?
- Possible inconsistency between `as.complex(NA_real_)` and the docs
- Status of reserved keywords and builtins