“==” not working??? Not a differnt type or

Anyone know how to fix this problem? Literally dying here.

I divide x by some number y and get the output Tmin.
But when I multiply Tmin by y to x again. R tells me that this is not x. However, when I compute the Tmin * y and assign the output by hand. R says that they are equal.

I checked, this is not because of different data types nor because of factors?
Tmin is obtained like this

test = structure(list(datetime = structure(c(1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 
1621506300, 1621506300), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), expire_mins = c(49235, 49235, 49235, 49235, 49235, 
49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 
49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 
49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 
49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 
49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 
180275, 180275, 180275, 180275, 180275, 180275, 180275, 180275, 
180275, 180275, 180275, 180275, 180275, 180275, 180275, 180275, 
180275, 180275, 180275, 180275, 180275, 180275, 49235, 49235, 
180275, 180275, 180275, 180275, 180275, 180275, 8915, 8915, 8915, 
8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 
8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 180275, 180275, 
311315, 311315, 311315, 311315, 311315, 311315, 311315, 311315, 
311315, 311315, 311315, 311315, 311315, 311315, 311315, 311315, 
311315, 311315, 311315, 311315), absdiff = c(0.54615, 0.4479, 
0.3504, 0.25145, 0.15385, 0.055, 0.04345, 0.14125, 0.23985, 0.54615, 
0.4479, 0.3504, 0.25145, 0.15385, 0.055, 0.04345, 0.14125, 0.23985, 
0.59595, 0.59595, 0.3384, 0.3384, 0.4049, 0.3054, 0.20485, 0.10475, 
0.00555, 0.0943, 0.1943, 0.29385, 0.39395, 0.4049, 0.3054, 0.20485, 
0.10475, 0.00555, 0.0943, 0.1943, 0.29385, 0.39395, 0.49335, 
0.49335, 0.50365, 0.50365, 0.5933, 0.5933, 0.69315, 0.69315, 
0.7936, 0.7936, 0.09445, 0.00419999999999998, 0.1033, 0.20315, 
0.30265, 0.4015, 0.50065, 0.5989, 0.6977, 0.09445, 0.00419999999999998, 
0.1033, 0.20315, 0.30265, 0.4015, 0.50065, 0.5989, 0.6977, 0.19435, 
0.19435, 0.79695, 0.79695, 0.8938, 0.8938, 0.89555, 0.89555, 
0.2928, 0.2928, 0.39175, 0.39175, 0.40115, 0.3015, 0.2017, 0.102, 
0.00219999999999999, 0.09815, 0.1982, 0.2983, 0.3983, 0.40115, 
0.3015, 0.2017, 0.102, 0.00219999999999999, 0.09815, 0.1982, 
0.2983, 0.3983, 0.49815, 0.49815, 0.501, 0.501, 0.4912, 0.4912, 
0.3695, 0.2717, 0.17305, 0.0744, 0.02405, 0.1227, 0.22075, 0.31945, 
0.41775, 0.3695, 0.2717, 0.17305, 0.0744, 0.02405, 0.1227, 0.22075, 
0.31945, 0.41775, 0.46775, 0.46775)), row.names = c(NA, -124L
), class = c("tbl_df", "tbl", "data.frame"))

N365 = 60*24*365
day8min = 60*24*7
Tfa = unique(test$expire_mins) %>% sort(decreasing = FALSE)
Tfa = Tfa[Tfa > day8min] %>% head(2)
Tmin = Tfa/N365
nx = test %>% subset(test$expire_mins == (Tmin[2] * N365))
# nx returned is of 0 obs and 9 variables

x <- 1:24
Tmin <- x / (24*60*365) # use <- to create a new object, = to set a parameter
Tmin
#>  [1] 1.902588e-06 3.805175e-06 5.707763e-06 7.610350e-06 9.512938e-06
#>  [6] 1.141553e-05 1.331811e-05 1.522070e-05 1.712329e-05 1.902588e-05
#> [11] 2.092846e-05 2.283105e-05 2.473364e-05 2.663623e-05 2.853881e-05
#> [16] 3.044140e-05 3.234399e-05 3.424658e-05 3.614916e-05 3.805175e-05
#> [21] 3.995434e-05 4.185693e-05 4.375951e-05 4.566210e-05

See the FAQ: How to do a minimal reproducible example reprex for beginners to repose the question in terms of Tmin.

No, I actually want to compare the second element of Tmin to the other number. It works fine for all other 200ish data points, produce this error for exactly two of them. Thx for the help though :wink:

What is not working exactly ? you didnt say...

Sorry. So I divide x by some number y and get the output Tmin.
But when I multiply Tmin by y to x again. R tells me that this is not x. However, when I compute the Tmin * y and assign the output by hand. R says that they are equal.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:


Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```
1 Like

Sorry. I updated the post. Thank you.

Thanks. The reprex below illustrates how to approach the unexpected result. The code works perfectly in that it returns an appropriate object with all the values that satisfy the == condition. Unfortunately, there are none. (Update: make that none visible as pointed out by @nutterb and @nirgrahamuk.)

library(magrittr)
test <- structure(list(datetime = structure(c(
  1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300, 1621506300, 1621506300, 1621506300, 1621506300,
  1621506300, 1621506300
), tzone = "UTC", class = c(
  "POSIXct",
  "POSIXt"
)), expire_mins = c(
  49235, 49235, 49235, 49235, 49235,
  49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235,
  49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235,
  49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235,
  49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235,
  49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235, 49235,
  180275, 180275, 180275, 180275, 180275, 180275, 180275, 180275,
  180275, 180275, 180275, 180275, 180275, 180275, 180275, 180275,
  180275, 180275, 180275, 180275, 180275, 180275, 49235, 49235,
  180275, 180275, 180275, 180275, 180275, 180275, 8915, 8915, 8915,
  8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915,
  8915, 8915, 8915, 8915, 8915, 8915, 8915, 8915, 180275, 180275,
  311315, 311315, 311315, 311315, 311315, 311315, 311315, 311315,
  311315, 311315, 311315, 311315, 311315, 311315, 311315, 311315,
  311315, 311315, 311315, 311315
), absdiff = c(
  0.54615, 0.4479,
  0.3504, 0.25145, 0.15385, 0.055, 0.04345, 0.14125, 0.23985, 0.54615,
  0.4479, 0.3504, 0.25145, 0.15385, 0.055, 0.04345, 0.14125, 0.23985,
  0.59595, 0.59595, 0.3384, 0.3384, 0.4049, 0.3054, 0.20485, 0.10475,
  0.00555, 0.0943, 0.1943, 0.29385, 0.39395, 0.4049, 0.3054, 0.20485,
  0.10475, 0.00555, 0.0943, 0.1943, 0.29385, 0.39395, 0.49335,
  0.49335, 0.50365, 0.50365, 0.5933, 0.5933, 0.69315, 0.69315,
  0.7936, 0.7936, 0.09445, 0.00419999999999998, 0.1033, 0.20315,
  0.30265, 0.4015, 0.50065, 0.5989, 0.6977, 0.09445, 0.00419999999999998,
  0.1033, 0.20315, 0.30265, 0.4015, 0.50065, 0.5989, 0.6977, 0.19435,
  0.19435, 0.79695, 0.79695, 0.8938, 0.8938, 0.89555, 0.89555,
  0.2928, 0.2928, 0.39175, 0.39175, 0.40115, 0.3015, 0.2017, 0.102,
  0.00219999999999999, 0.09815, 0.1982, 0.2983, 0.3983, 0.40115,
  0.3015, 0.2017, 0.102, 0.00219999999999999, 0.09815, 0.1982,
  0.2983, 0.3983, 0.49815, 0.49815, 0.501, 0.501, 0.4912, 0.4912,
  0.3695, 0.2717, 0.17305, 0.0744, 0.02405, 0.1227, 0.22075, 0.31945,
  0.41775, 0.3695, 0.2717, 0.17305, 0.0744, 0.02405, 0.1227, 0.22075,
  0.31945, 0.41775, 0.46775, 0.46775
)), row.names = c(NA, -124L), class = c("tbl_df", "tbl", "data.frame"))

N365 <- 60 * 24 * 365
day8min <- 60 * 24 * 7
Tfa <- unique(test$expire_mins) %>% sort(decreasing = FALSE)
Tfa <- Tfa[Tfa > day8min] %>% head(2)
Tmin <- Tfa / N365

# returns an empty tibble
nx <- test %>% subset(test$expire_mins == (Tmin[2] * N365))

# check values of arguments separately

N365
#> [1] 525600
Tmin[2]
#> [1] 0.342989
N365*Tmin[2]
#> [1] 180275
test$expire_mins
#>   [1]  49235  49235  49235  49235  49235  49235  49235  49235  49235  49235
#>  [11]  49235  49235  49235  49235  49235  49235  49235  49235  49235  49235
#>  [21]  49235  49235  49235  49235  49235  49235  49235  49235  49235  49235
#>  [31]  49235  49235  49235  49235  49235  49235  49235  49235  49235  49235
#>  [41]  49235  49235  49235  49235  49235  49235  49235  49235  49235  49235
#>  [51] 180275 180275 180275 180275 180275 180275 180275 180275 180275 180275
#>  [61] 180275 180275 180275 180275 180275 180275 180275 180275 180275 180275
#>  [71] 180275 180275  49235  49235 180275 180275 180275 180275 180275 180275
#>  [81]   8915   8915   8915   8915   8915   8915   8915   8915   8915   8915
#>  [91]   8915   8915   8915   8915   8915   8915   8915   8915   8915   8915
#> [101]   8915   8915 180275 180275 311315 311315 311315 311315 311315 311315
#> [111] 311315 311315 311315 311315 311315 311315 311315 311315 311315 311315
#> [121] 311315 311315 311315 311315

# create LHE and RHE
LHE <- test$expire_mins
RHE <- N365*Tmin[2]

# check to see if RHE occurs at all in LHE
RHE %in% LHE
#> [1] FALSE

The issue you're seeing is not that == isn't working, it's that you're dealing with floating point arithmetic. Event though R prints to the console 180275 for both Tmin[2] * N365 and test$expire_mins[52], they are not actually the same value. Try running both of these:

sprintf("%0.20f", Tmin[2] * N365)        # > "180274.99999999997089616954"
sprintf("%0.20f", test$expire_mins[52])  # > "180275.00000000000000000000"

To do your comparison, you'll need to use some combination of all.equal and isTRUE. I would recommend making your own comparison function, such as

are_they_equal <- function(x, y, ...){
  y <- rep(y, length.out = length(x))
  out <- mapply(function(x, y, ...){ isTRUE(all.equal(x, y, ...))}, 
                x  = x, 
                y = y, 
                ..., 
                SIMPLIFY = FALSE)
  unlist(out)
}

If you're interested in reading more on this, this is a pretty good review of the subject r - Why are these numbers not equal? - Stack Overflow

3 Likes

dplyr has a near function which is vectorised

dplyr::near(x=c(1,1.00001),
            y=c(1.00001,1.00001))
[1] FALSE  TRUE

dplyr::near(x=c(1,1.00001),
            y=c(1.00001,1.00001),
            tol=0.0001)
[1] TRUE TRUE
4 Likes

Don't be like me. near looks nice. :smiley:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.