【问题标题】:Error in full_join in dplyr with a POSIXct object带有 POSIXct 对象的 dplyr 中的 full_join 错误
【发布时间】:2023-03-24 02:20:01
【问题描述】:

我有两个要合并的数据框(称为d1small)。我导出了每个数据框并使其可用here

d1 数据帧用于生成 small 数据帧。我使用了一系列for if 循环来确定d1 数据集中每个物种(sps)的存在/不存在(在两个小时的区间内),以生成small 数据集。

我想做的是从small 中取出TRUE/FALSE 行并将它们与d1 合并以获得类似的东西(假设示例):

         datetime     MUVI80 MUXX80 MICRO80 TAHU80 TAST80 ERDO80 LEAM80 ONZI80 MEME80 MAMO80 sps  pp      datetime        km crossingtype
1 2012-06-19 01:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE  FALSE MICRO  0  2012-06-19 02:19    80  Exploration
2 2012-06-21 21:42:00  FALSE   TRUE   FALSE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE  FALSE MUXX   1  2012-06-21 23:23    80      Unknown
3 2012-07-15 09:42:00  FALSE  FALSE   FALSE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE   TRUE MAMO   0  2012-07-15 11:38    80     Complete
4 2012-07-20 21:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE  FALSE MICRO  0  2012-07-20 22:19    80  Exploration
5 2012-07-29 21:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE  FALSE MICRO  0  2012-07-29 23:03    80  Exploration
6 2012-08-08 23:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE FALSE  FALSE  FALSE MICRO  0  2012-08-07 02:04    80     Complete

虽然两个数据集共享一个公共字段 datetime,但它们的格式不同,这导致问题有两个原因:

  1. datetime 字段是 small 中的 POSIXct 对象,但不在 d1 中。
  2. 为了在 small 中创建 datetime 字段,我还设置了 2 小时的时间段(即我问,在两小时内,物种是存在 (TRUE) 还是不存在 (FALSE))。这意味着datetime 字段在smalld1 数据集之间不会完全匹配。相反,d1 中的 datetime 字段位于 small 中的 datetime 字段 2 小时内。

因此,当我尝试时:

time<-dplyr::full_join(small, d1, by = "datetime")

显然不行。

我得到的错误如下:

Error in full_join_impl(x, y, by$x, by$y, suffix$x, suffix$y, check_na_matches(na_matches)) : cannot join a POSIXct object with an object that is not a POSIXct object

有人对我的方法有什么建议吗:

  1. 检查不同的datetime 字段的格式,然后将它们强制转换为相同的格式。
  2. 合并这两个数据集(尽管datetime 字段中的小时不匹配)。

【问题讨论】:

  • 你能分享这两个数据集吗?
  • 请注意,将datetime 字段强制转换为相同格式的更好方法是:datetime=ymd_hm(datetime) 来自lubridate 包。

标签: r datetime merge dplyr time-series


【解决方案1】:

sqldf 提供了处理基于range 的data.frame 和表连接的场景的灵活性。让我演示一下sqldf 可以用来解决OP 中提到的问题的方式。

I started with reading data from files shared in OP. 

library(sqldf)

# Read the data from d1.txt. Pretty straight forward.
d1 <- read.table("d1.txt", header = TRUE, stringsAsFactors = FALSE)

# The datetime column is character. Hence change it to POSIXct
d1$datetime <- as.POSIXct(d1$datetime)

# small.txt file doesn't contain datetime together. Need to introduce 
# another column as onlytime to read time part separately. 
small <- read.table("small.txt", header = TRUE, stringsAsFactors = FALSE)

# merge onlytime part with date part in datetime column
small$datetime = paste(small$datetime, small$onlytime, sep = " ")
# drop column onlytime
small$onlytime <- NULL
# Now datetime column is character. Hence change it to POSIXct
small$datetime <- as.POSIXct(small$datetime)

# everything is ready now. Lets join two dataframes
# small$datetime is at 2 hours interval and represent data for past 2 hours
# Hence range matching records to be found within 2 hours(2*60*60) before and 
# time of current row

time = sqldf("select * from d1
                inner join small
               on d1.datetime between (small.datetime - 2*60*60) and small.datetime")


head(time, 3)
     ID       date   sps  time pp            datetime km crossingtype            datetime MUVI80 MUXX80 MICRO80 TAHU80 TAST80 ERDO80 LEAM80 ONZI80
1 15185 2012-10-22 MICRO  3:42  0 2012-10-22 03:42:00 80      Unknown 2012-10-22 03:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE  FALSE
2 15187 2012-10-23 MICRO  0:40  0 2012-10-23 00:40:00 80      Unknown 2012-10-23 01:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE  FALSE
3 17018 2012-10-29 MICRO 21:03  0 2012-10-29 21:03:00 80      Unknown 2012-10-29 21:42:00  FALSE  FALSE    TRUE  FALSE  FALSE  FALSE  FALSE  FALSE

可以更改联接类型以适应 OP 中的实际对象。

【讨论】:

  • 这是完美的。它正是我需要的。感谢您的明确答复!
猜你喜欢
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 2016-07-11
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多