0

I am trying to compare columns from two different tibbles to find the corresponding row in dat2 to fill. However, I keep getting a integer(0) result for the second part of the which function. The two tibbles contain columns of lon, lat, values/0, where values from the first data frame dat will fill the full-length tibbledat2 based on matching lon and lat values.

dat originates from a .nc file that was opened using: raster::brick. Where after I treated it like a raster and aggregated the data to be on a 1x1 degree spacing, I made sure that the lon and lat columns were the same data class and numeric values. For example, dat$lon ceiling(-179.5) to get -179 matching dat2$V1 and 0.25 was added to dat$lat to get the same values of dat2$V2. Then applying the which().

The which() line in question:

which(dat$lon[k] == dat2$V1 & dat$lat[k] == dat2$V2)

I was able to get the first part of the which statement to work, when testing it individually. However, the second statement has been tripping me up for a bit now now.

Header of dat:

head(dat)
# A tibble: 6 × 3
    lon   lat    V3
  <dbl> <dbl> <dbl>
1   -82  76.5   179
2   -95  75.5     9
3   -94  75.5    44
4   -95  74.5    15
5   -94  74.5   177
6   -85  73.5   848

Head of dat2:

A tibble: 6 × 3
     V1    V2    V3
  <dbl> <dbl> <dbl>
1     0 -89.5     0
2     1 -89.5     0
3     2 -89.5     0
4     3 -89.5     0
5     4 -89.5     0
6     5 -89.5     0

Row 1 of dat should correspond with row 60039 of dat2:

dat2[60037:60040,]
# A tibble: 4 × 3
     V1    V2    V3
  <dbl> <dbl> <dbl>
60037   -84  76.5     0
60038   -83  76.5     0
60039   -82  76.5     0
60040   -81  76.5     0

I was getting this issue previously but was able to find a different, more efficient workaround. However, this time I am at a loss for a workaround that will work and/or solving this issue. I think it is a data type issue that is inherited from the raster data. Even though class() is numeric for the columns in question. ceiling() results in it working but + 0.25 does not.

I have tried to apply unlist() and as.numeric() to the dat$lat column and I have also tried trimws() on a whim from this previous question. All to no avail.

Most recently tested the floating point as suggested. Both all.equal(dat$lat[1], 76.5) and all.equal(dat2$V2[60039], 76.5) result in TRUE. Still, which() does not work. And:

all.equal(dat$lat[1], dat2$V2[60039])
[1] TRUE
tannie
  • 1
  • 1
  • 5
    [Probably relevant](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Limey Jun 12 '23 at 19:04
  • @Limey Thanks, unfortunately, didn't help. – tannie Jun 12 '23 at 20:15
  • 1
    That link should have let you to conclude that you cannot do exact matches for double numeric values. You cannot use `which()`. Floating point numbers are just too imprecise. You would need to consistently `round()` them to the same decimal place, or if you only want to match to the first decimal point, multiple by 100 and then turn into an integer rather than leaving them as a floating point – MrFlick Jun 12 '23 at 20:19
  • I previously tried that (went to the floating point guide), and it didn't work. Previous to trying this I also updated RStudio and it kept [throwing errors](https://github.com/rstudio/rstudio/issues/13188). I'm sure I missed something because of the error. Redownloaded the previous version, and now multiplying by 100 seems to work. Thanks, @MrFlick for making me try this method again, it's been a long day. – tannie Jun 12 '23 at 21:01
  • 1
    Please edit your question and include a minimal, self-contained, reproducible example. You need to create some example data. E.g. `dat2 <- data.frame(lon=c(1,2), lat=c(1/3, 1/4))` – Robert Hijmans Jun 12 '23 at 21:29
  • Does this answer your question? [Why are these numbers not equal?](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Limey Jun 12 '23 at 22:18
  • Your response to @MrFlick indicates that the link I provided _should have_ helped. – Limey Jun 13 '23 at 08:02

0 Answers0