Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pmin gives the wrong answer

Tags:

dataframe

r

pmin does not return the appropriate output in the following example.

eps <- 1e-16
x <- structure(list(X = c(0.219801587301587, 0.340792857142857, 0.398129365079365, 
                      1, 1, 0.853353968253968, 0.930726984126984, 0.980263131313131, 
                      0.968269047619047, 0.953053336369513, 1, 1, 1, 0.951969003219003, 
                      0.91514335177894, 0.884824997224998, 0.884824997224998, 0.884824997224998 )), row.names = c(NA, 18L), class = "data.frame", .Names = "X")

pmin(x, 1 - eps)

The function incorrectly returns NA where the value of x is 1. Should this be reported as a bug?

like image 853
student Avatar asked Jun 23 '17 00:06

student


1 Answers

So I figured out that this is because x is a data.frame and pmin is expecting a vector. So the following works just fine:

pmin(x[,1], 1 - eps)

But I wanted to understand why it wasn't working for data.frame. So going through the code for pmin the offending line is the following:

mmm[change] <- each[change]

because each refers to the constant (1 - eps in this case) and change is a logical vector anything other than each[1] will return NA. Whereas the case where x is a vector executes at the beginning of the function at:

  if (all(vapply(elts, function(x) is.atomic(x) && !is.object(x), NA))) {
      mmm <- .Internal(pmin(na.rm, ...))
      mostattributes(mmm) <- attributes(elts[[1L]])
   }

That part of the code it seems is intended for when the values for pmin are vectors and the latter part takes care of the case where you might have a data.frame, but it fails in this given scenario.

like image 149
student Avatar answered Nov 10 '22 13:11

student