max() function in R returned a value that did not equal to itself

by weichen song   Last Updated August 14, 2019 18:26 PM

I used max(d[,2:3]) to extract the largest number of two columns of a data frame, but I got really weird value:

> head(D)
          CD41 label clusterID
1 0.0011454440     7         5
2 1.0996334553    10         4
3 0.0011956851     9        12
4 0.0992929861    13        12
5 0.0008555306     7         5
6 0.0001552506     7         5
> d=head(D)
> max(d[,2:3])
[1] 13
> 1:max(d[,2:3])
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> max(d[,2:3])
[1] 13
> max(d[,2:3])==13
[1] FALSE
> max(d[,2:3])==12
[1] FALSE
> is.numeric(max(d[,2:3]))
[1] TRUE

How could a numeric "13" not equal to 13? I'm really puzzled. Thanks in advance for any help.

Tags : r


Answers 1


The reason it didn't work may be due to the floating point. It may be that the values having some precision.

round(max(d[,2:3]))==13

Note that the OP subset only the head of the dataset. The columns could be numeric class even though it is appearing as integer class in the head. This means that if the value is 13.000000001 or 12.9999999 it won't be equal to 13.


By copying the data, it gives the correct output

max(d[,2:3])==13
#[1] TRUE

data

d <- structure(list(CD41 = c(0.001145444, 1.0996334553, 0.0011956851, 
0.0992929861, 0.0008555306, 0.0001552506), label = c(7L, 10L, 
9L, 13L, 7L, 7L), clusterID = c(5L, 4L, 12L, 12L, 5L, 5L)),
class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
akrun
akrun
July 30, 2019 01:17 AM

Related Questions