日期时间戳在 R 中不匹配/正确合并

date timestamps not matching / merging properly in R

我有两个数据框:

locA:

Date    Time    Temperature Voltage Longitude   Latitude    Deployment  DateTime
2014-08-08  14:18:25    20.9    1.872   -126.6886   34.95633    A   2014-08-08 14:20:00
2014-08-08  14:38:25    19.8    1.862   -126.6899   34.95675    A   2014-08-08 14:40:00
2014-08-08  14:58:26    19.3    1.856   -126.6911   34.95732    A   2014-08-08 15:00:00
2014-08-08  15:18:26    18.8    1.872   -126.6921   34.95803    A   2014-08-08 15:20:00
2014-08-08  15:38:25    18.6    1.872   -126.6929   34.95872    A   2014-08-08 15:40:00
2014-08-08  15:58:26    18.4    1.859   -126.6937   34.95921    A   2014-08-08 16:00:00
2014-08-08  16:18:25    18.4    1.875   -126.6944   34.95983    A   2014-08-08 16:20:00
2014-08-08  16:38:25    18.4    1.875   -126.6949   34.96021    A   2014-08-08 16:40:00
2014-08-08  16:58:26    18.4    1.872   -126.6956   34.96033    A   2014-08-08 17:00:00
2014-08-08  17:18:26    18.4    1.872   -126.6962   34.96025    A   2014-08-08 17:20:00

和数据A:

FileName    Deployment  Start_UTC   Stop_UTC    Duration    sum PrAb    dolPrAb
DASBR1_201408080000.wav  A   2014-08-08 14:00:00 2014-08-08 14:02:00 119.6000    0   0   0
DASBR1_201408082000.wav  A   2014-08-08 14:20:00 2014-08-08 14:22:00 119.6000    0   0   0
DASBR1_201408084000.wav  A   2014-08-08 14:40:00 2014-08-08 14:42:00 119.8000    0   0   0
DASBR1_201408080000.wav  A   2014-08-08 15:00:00 2014-08-08 15:02:00 119.6000    0   0   0
DASBR1_201408082000.wav  A   2014-08-08 15:20:00 2014-08-08 15:22:00 119.6000    0   0   0
DASBR1_201408084000.wav  A   2014-08-08 15:40:00 2014-08-08 15:42:00 119.6000    0   0   0
DASBR1_201408080000.wav  A   2014-08-08 16:00:00 2014-08-08 16:02:00 119.6000    1   1   1
DASBR1_201408082000.wav  A   2014-08-08 16:20:00 2014-08-08 16:22:00 119.8000    0   0   0
DASBR1_201408084000.wav  A   2014-08-08 16:40:00 2014-08-08 16:42:00 119.8000    0   0   0
DASBR1_201408080000.wav  A   2014-08-08 17:00:00 2014-08-08 17:02:00 119.6000    0   0   0

实际上每个都有几千行。我想根据 locA 中的 "DateTime" 标记和 dataA 中的 "Start_UTC" 合并它们。这两列在 UTC 中的格式相同。

locA$DateTime=as.POSIXct(paste(locA$Date, time), format="%Y-%m-%d %H:%M:%S", tz="UTC")
datA$Start_UTC = as.POSIXct(files$Start_UTC, format="%Y-%m-%d %H:%M:%S", tz="UTC")

在 locA 中,"time" 变量来自我将 locA$Time 中的字符串四舍五入到最接近的 20 分钟的序列。 "time"为调整后的时间字符串

str(time)
chr [1:8845] "14:00:00" "14:20:00" "14:40:00" "15:00:00" "15:20:00" "15:40:00" "16:00:00" "16:20:00" "16:40:00" "17:00:00" ...

当我使用 locA$DateTime 和 datA$Start_UTC 合并两个数据帧时,它永远无法正常工作。我每次都会得到看起来像这样的东西。

resA = merge(datA,locA, by.x=c("Start_UTC"), by.y=c("DateTime"))

resA:

Start_UTC   FileName    Deployment.x    Stop_UTC    Duration    sum PrAb    dolPrAb Date    Time    Temperature Voltage Longitude   Latitude    Deployment.y
2014-08-08 14:00:00 DASBR1_201408080000.wav  A   2014-08-08 14:02:00 119.6000    0   0   0   2014-08-08  20:58:26    19.1    1.872   -126.7018   34.94994    A
2014-08-08 14:20:00 DASBR1_201408082000.wav  A   2014-08-08 14:22:00 119.6000    0   0   0   2014-08-08  21:18:26    19.1    1.872   -126.7027   34.94801    A
2014-08-08 14:40:00 DASBR1_201408084000.wav  A   2014-08-08 14:42:00 119.8000    0   0   0   2014-08-08  21:38:26    19.1    1.872   -126.7038   34.94608    A
2014-08-08 15:00:00 DASBR1_201408080000.wav  A   2014-08-08 15:02:00 119.6000    0   0   0   2014-08-08  21:58:27    18.9    1.865   -126.7050   34.94394    A
2014-08-08 15:20:00 DASBR1_201408082000.wav  A   2014-08-08 15:22:00 119.6000    0   0   0   2014-08-08  22:18:27    18.9    1.856   -126.7062   34.94188    A
2014-08-08 15:40:00 DASBR1_201408084000.wav  A   2014-08-08 15:42:00 119.6000    0   0   0   2014-08-08  22:38:26    18.8    1.862   -126.7077   34.93994    A
2014-08-08 16:00:00 DASBR1_201408080000.wav  A   2014-08-08 16:02:00 119.6000    1   1   1   2014-08-08  22:58:26    18.8    1.859   -126.7090   34.93796    A
2014-08-08 16:20:00 DASBR1_201408082000.wav  A   2014-08-08 16:22:00 119.8000    0   0   0   2014-08-08  23:18:26    18.8    1.865   -126.7105   34.93622    A
2014-08-08 16:40:00 DASBR1_201408084000.wav  A   2014-08-08 16:42:00 119.8000    0   0   0   2014-08-08  23:38:27    18.6    1.856   -126.7120   34.93480    A
2014-08-08 17:00:00 DASBR1_201408080000.wav  A   2014-08-08 17:02:00 119.6000    0   0   0   2014-08-09  23:58:28    18.8    1.849   -126.8051   34.88381    A

我已经尝试了很多不同的方法。我尝试保持 locA$DateTime 不变,其中时间等于 datA$Time 中的时间戳,然后尝试使用此函数将其与正确的 datA$Start_UTC 匹配。将最小的 locA$timestamp 匹配到 datA$timestamp

findrow=function(dt,df) {min(which(df>dt))}
rowA=sapply(locA$DateTime, findrow, df=datA$Start_UTC)
resA=cbind(locA,datA[rowA,])

这给了我相同的结果。我不知道发生了什么。我确定这是生成日期时间戳的固有方式,但我不知道如何解决这个问题。如果我将它们格式化为相同的格式,并告诉 R 它们是 UTC,它们不应该以相同的方式对待吗?

请帮助,所有帮助、评论或幽默的 "hang in there" 陈述将不胜感激。无法将我的 lat/longs 与我的数据时间戳正确配对确实令人头疼。

干杯。

我想知道对 chr 对象而不是 POSIXct 对象进行合并是否会更好?

locA$DateTime_str = format(locA$DateTime)
datA$Start_UTC_str = format(datA$Start_UTC)
resA = merge(datA,locA, by.x=c("Start_UTC_str"), by.y=c("DateTime_str"))