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
