2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

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
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for your help, i tried m=max(as.matrix(d[,2:3])), but 1:m still returned 1 to 12 – weichen song Jul 30 '19 at 01:22
  • @weichensong. Do you see `d$label[4] - 13` not equal to 0 – akrun Jul 30 '19 at 01:28
  • 1
    Thank you very much for your guidance! Now it turned out to be the problem of floating points. In earlier steps of definition of data frame D, i added a ceiling() step and it worked well now. – weichen song Jul 30 '19 at 02:22