按确切时间过滤或匹配

Filtering or matching by exact time

当我尝试按精确时间过滤 df 时,而不是在两个不同时间之间,它会生成 NA。我正在尝试根据 df2 中的信息在 df1 中创建一个变量。

这是我的数据

dput(df2)
structure(list(Time = structure(c(1647531450.72, 1647531451.757, 
1647531452.794, 1647531453.83, 1647531454.867, 1647531455.818, 
1647531456.854, 1647531457.891, 1647531458.928, 1647531459.878, 
1647531460.915, 1647531461.952, 1647531462.902, 1647531463.939, 
1647531464.976, 1647531466.013, 1647531467.05, 1647531468, 1647531469.037, 
1647531470.074, 1647531471.11, 1647531472.147, 1647531473.098, 
1647531474.134, 1647531475.171, 1647531476.208, 1647531477.245, 
1647531478.195, 1647531479.232, 1647531480.269, 1647531481.306, 
1647531482.342, 1647531483.293, 1647531484.33, 1647531485.366, 
1647531486.317, 1647531487.354, 1647531488.39, 1647531489.427, 
1647531490.378, 1647531491.414, 1647531492.451, 1647531493.488, 
1647531494.438, 1647531495.475, 1647531496.512, 1647531497.549, 
1647531498.586, 1647531499.536, 1647531500.573, 1647531501.61, 
1647531502.56, 1647531503.597, 1647531504.634, 1647531505.67, 
1647531506.621, 1647531507.658, 1647531508.694, 1647531509.645
), tzone = "", class = c("POSIXct", "POSIXt")), LAT = c(17.8799454, 
17.8799729, 17.8799952, 17.8800159, 17.8800416, 17.8800708, 17.8801, 
17.8801292, 17.8801567, 17.8801877, 17.8802237, 17.8802581, 17.8802873, 
17.8803148, 17.8803474, 17.8803818, 17.8804161, 17.8804471, 17.8804763, 
17.8805089, 17.8805381, 17.880569, 17.8806034, 17.880636, 17.8806721, 
17.8807048, 17.8807374, 17.8808061, 17.8808405, 17.8808783, 17.8808783, 
17.8809556, 17.8809968, 17.8810346, 17.8810724, 17.8810724, 17.8811497, 
17.8811892, 17.8812288, 17.88127, 17.8813112, 17.8813524, 17.8813954, 
17.8814383, 17.8814813, 17.8815208, 17.8815603, 17.8815964, 17.8816359, 
17.8816737, 17.8817132, 17.8817562, 17.8817974, 17.8818438, 17.8818885, 
17.8819314, 17.8819726, 17.8820104, 17.88205), LON = c(-62.8613544, 
-62.8613338, -62.8613063, -62.8612857, -62.861265, -62.8612513, 
-62.8612307, -62.8612101, -62.8611894, -62.8611757, -62.8611688, 
-62.8611482, -62.8611276, -62.8611139, -62.8611001, -62.8610795, 
-62.8610658, -62.861052, -62.8610314, -62.8610176, -62.860997, 
-62.8609833, -62.8609627, -62.8609489, -62.8609352, -62.8609214, 
-62.8609008, -62.8608733, -62.8608665, -62.8608596, -62.8608596, 
-62.8608459, -62.860839, -62.8608321, -62.8608252, -62.8608252, 
-62.8608115, -62.8608115, -62.8608115, -62.8608115, -62.8608115, 
-62.8608115, -62.8608115, -62.8608115, -62.8608115, -62.8608046, 
-62.8607977, -62.8607909, -62.860784, -62.8607771, -62.8607771, 
-62.8607771, -62.8607771, -62.8607771, -62.8607771, -62.8607703, 
-62.8607634, -62.8607496, -62.8607428)), class = "data.frame", row.names = c(NA, 
-59L))

和包含我要过滤的信息的 df

dput(df1)
structure(list(date = structure(19068, class = "Date"), RaceStartTime = structure(1647531480, tzone = "", class = c("POSIXct", 
"POSIXt"))), class = "data.frame", row.names = "event.2")

我试过以下方法

df1$lon <- df2$LON[match(df1$RaceStartTime, df2$Time)]

我也试过了

df1$lon <- df2%>%
  filter(Time == df1$RaceStartTime)

这两个都产生空行,有人能指出明显的错误吗?!

编辑: 结构看起来相同

str(df1$RaceStartTime)
 POSIXct[1:1], format: "2022-03-17 15:38:00"

str(df2$Time)
 POSIXct[1:59], format: "2022-03-17 15:37:30"

谢谢

POSIXct 格式默认只打印整秒,但它的底层表示可以包含小数秒(就像 df2 中的数据一样)。

您可以通过以下操作删除小数秒:

df2$Time <- lubridate::floor_date(df2$Time)

现在你得到:

df2%>%
     filter(Time == df1$RaceStartTime)
#>                  Time      LAT       LON
#> 1 2022-03-17 15:38:00 17.88088 -62.86086