Home > Enterprise >  Subsetting does not work in numeric values
Subsetting does not work in numeric values

Time:08-27

I have a large dataframe (t2m.all) with longitude (lon), latitude (lat) and hourly temperatures since 1958 (X1958.01.01.00.00.00). A subset of the data is given in the code below:

dput(t2m.all[1:300,1:3])
structure(list(Lon = c(-102, -101.9, -101.8, -101.7, -101.6, 
-101.5, -101.4, -101.3, -101.2, -101.1, -101, -100.9, -100.8, 
-100.7, -100.6, -100.5, -100.4, -100.3, -100.2, -100.1, -100, 
-99.9, -99.8, -99.7, -99.6, -99.5, -99.4, -99.3, -99.2, -99.1, 
-99, -98.9, -98.8, -98.7, -98.6, -98.5, -98.4, -98.3, -98.2, 
-98.1, -98, -97.9, -97.8, -97.7, -97.6, -97.5, -97.4, -97.3, 
-97.2, -97.1, -97, -96.9, -96.8, -96.7, -96.6, -96.5, -96.4, 
-96.3, -96.2, -96.1, -96, -95.9, -95.8, -95.7, -95.6, -95.5, 
-95.4, -95.3, -95.2, -95.1, -95, -102, -101.9, -101.8, -101.7, 
-101.6, -101.5, -101.4, -101.3, -101.2, -101.1, -101, -100.9, 
-100.8, -100.7, -100.6, -100.5, -100.4, -100.3, -100.2, -100.1, 
-100, -99.9, -99.8, -99.7, -99.6, -99.5, -99.4, -99.3, -99.2, 
-99.1, -99, -98.9, -98.8, -98.7, -98.6, -98.5, -98.4, -98.3, 
-98.2, -98.1, -98, -97.9, -97.8, -97.7, -97.6, -97.5, -97.4, 
-97.3, -97.2, -97.1, -97, -96.9, -96.8, -96.7, -96.6, -96.5, 
-96.4, -96.3, -96.2, -96.1, -96, -95.9, -95.8, -95.7, -95.6, 
-95.5, -95.4, -95.3, -95.2, -95.1, -95, -102, -101.9, -101.8, 
-101.7, -101.6, -101.5, -101.4, -101.3, -101.2, -101.1, -101, 
-100.9, -100.8, -100.7, -100.6, -100.5, -100.4, -100.3, -100.2, 
-100.1, -100, -99.9, -99.8, -99.7, -99.6, -99.5, -99.4, -99.3, 
-99.2, -99.1, -99, -98.9, -98.8, -98.7, -98.6, -98.5, -98.4, 
-98.3, -98.2, -98.1, -98, -97.9, -97.8, -97.7, -97.6, -97.5, 
-97.4, -97.3, -97.2, -97.1, -97, -96.9, -96.8, -96.7, -96.6, 
-96.5, -96.4, -96.3, -96.2, -96.1, -96, -95.9, -95.8, -95.7, 
-95.6, -95.5, -95.4, -95.3, -95.2, -95.1, -95, -94.9, -102, -101.9, 
-101.8, -101.7, -101.6, -101.5, -101.4, -101.3, -101.2, -101.1, 
-101, -100.9, -100.8, -100.7, -100.6, -100.5, -100.4, -100.3, 
-100.2, -100.1, -100, -99.9, -99.8, -99.7, -99.6, -99.5, -99.4, 
-99.3, -99.2, -99.1, -99, -98.9, -98.8, -98.7, -98.6, -98.5, 
-98.4, -98.3, -98.2, -98.1, -98, -97.9, -97.8, -97.7, -97.6, 
-97.5, -97.4, -97.3, -97.2, -97.1, -97, -96.9, -96.8, -96.7, 
-96.6, -96.5, -96.4, -96.3, -96.2, -96.1, -96, -95.9, -95.8, 
-95.7, -95.6, -95.5, -95.4, -95.3, -95.2, -95.1, -95, -94.9, 
-102, -101.9, -101.8, -101.7, -101.6, -101.5, -101.4, -101.3, 
-101.2, -101.1, -101, -100.9, -100.8, -100.7), Lat = c(60, 60, 
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
60, 60, 60, 60, 60, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 
59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.9, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 59.8, 
59.8, 59.8, 59.8, 59.8, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 
59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.7, 59.6, 
59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 59.6, 
59.6, 59.6), X1958.01.01.00.00.00 = c(-37.2, -37.2, -36.6, -35.9, 
-36, -36.1, -35.8, -35.5, -35.1, -34.7, -34.2, -33.6, -33.6, 
-34, -33.8, -33.2, -32.8, -32.6, -32.5, -32.5, -32.4, -32.3, 
-32.3, -32.7, -33.1, -33.2, -33.3, -33.3, -33.3, -33.1, -32.9, 
-32.6, -32.4, -32.1, -31.8, -31.5, -31.2, -30.9, -30.6, -30.3, 
-30, -29.7, -29.4, -29.1, -28.8, -28.5, -28.2, -27.9, -27.7, 
-27.4, -27.1, -26.8, -26.6, -26.3, -26.1, -25.9, -25.6, -25.4, 
-25.2, -24.9, -24.7, -24.4, -24.1, -23.8, -23.6, -23.3, -23, 
-22.7, -22.4, -22.1, -21.9, -36.6, -37.1, -36.6, -36.1, -36, 
-36.1, -35.7, -35, -34.6, -34.8, -34.7, -34, -33.8, -33.7, -33.6, 
-33.2, -32.4, -32.3, -32.3, -32.3, -32.3, -32.8, -32.9, -32.7, 
-33, -33.1, -33.1, -33.1, -33.1, -33, -32.8, -32.5, -32.3, -32.1, 
-31.8, -31.5, -31.2, -30.9, -30.6, -30.3, -30.1, -29.8, -29.4, 
-29.1, -28.8, -28.5, -28.3, -28, -27.7, -27.4, -27.1, -26.9, 
-26.6, -26.4, -26.1, -25.9, -25.6, -25.4, -25.2, -24.9, -24.7, 
-24.4, -24.1, -23.9, -23.6, -23.3, -23, -22.8, -22.5, -22.2, 
-21.9, -36.7, -36.8, -36.8, -36.4, -36.2, -36, -35.9, -35.2, 
-34.6, -34.6, -34.4, -34.2, -34.2, -33.8, -33.3, -32.7, -32.3, 
-32.2, -32.3, -32.6, -33.3, -33.5, -33.2, -33.1, -32.5, -32.2, 
-32.7, -33, -32.9, -32.8, -32.6, -32.5, -32.2, -32, -31.7, -31.5, 
-31.2, -30.9, -30.6, -30.4, -30.1, -29.8, -29.5, -29.2, -28.9, 
-28.6, -28.3, -28, -27.7, -27.4, -27.2, -26.9, -26.7, -26.4, 
-26.2, -25.9, -25.7, -25.4, -25.2, -24.9, -24.7, -24.4, -24.2, 
-23.9, -23.6, -23.4, -23.1, -22.8, -22.5, -22.3, -22, -21.9, 
-36.6, -36.6, -36.5, -36.3, -36.2, -35.7, -35.1, -34.5, -34.1, 
-34.5, -34.7, -34, -33.3, -33, -32.6, -32.6, -32.8, -33, -33.3, 
-33.2, -32.9, -32.9, -33, -32.9, -33, -33.1, -33, -33, -32.9, 
-32.8, -32.6, -32.4, -32.2, -32, -31.7, -31.4, -31.2, -31, -30.7, 
-30.4, -30.1, -29.8, -29.4, -29.1, -28.8, -28.5, -28.3, -28, 
-27.8, -27.5, -27.2, -26.9, -26.7, -26.4, -26.1, -25.9, -25.6, 
-25.4, -25.2, -24.9, -24.7, -24.4, -24.2, -23.9, -23.6, -23.4, 
-23.1, -22.9, -22.6, -22.3, -22, -21.9, -36.4, -36.3, -36, -35.7, 
-35.3, -34.5, -34.3, -34.5, -34.5, -34.3, -33.9, -33.5, -33.2, 
-32.9)), row.names = c(NA, 300L), class = "data.frame")

I wish to subset t2m.all to isolate a single row returning the temperature value for a specified latitude and longitude pairing. This works for some coordinates but strangely not for others. For example, the below code works for these coordinates:

res = subset(t2m.all, Lon == -100.7 & Lat == 59.8)

but does not work for these coordinates:

res = subset(t2m.all, Lon == -100.7 & Lat == 59.6)

Both set of coordinates are clearly in the t2m.all dataframe, so why can R subset for some sets of coordinates but not for others? The logic seems sound to me, so I'm not sure what can be wrong. I have spent a long time going through all possibilities and troubleshooting on Stackoverflow, but I have yet to find a solution. Any suggestions? Many thanks in advance.

CodePudding user response:

The issue results from round-off errors. You could set a tolerance value when comparing Lon & Lat with a certain value. In base you could use abs(x-y) < 1e-5 to achieve it:

t2m.all |>
  subset(abs(Lon - -100.7) < 1e-5 & abs(Lat - 59.6) < 1e-5)

#      Lon  Lat X1958.01.01.00.00.00
# 1 -100.7 59.6                -32.9

The dplyr equivalent is near():

library(dplyr)

t2m.all %>% 
  filter(near(Lon, -100.7) & near(Lat, 59.6))

#      Lon  Lat X1958.01.01.00.00.00
# 1 -100.7 59.6                -32.9
  • Related