我正在尝试使用'fuzzyjoin:interval_inner_join'将R中的两个大型数据集连接起来。我的目标是根据最近的开始和结束时间将它们加入表中。
# first dataset
viewing <- data.frame(stringsAsFactors=FALSE,
id = c("100-16", "100-16", "100-16", "100-16", "100-16",
"100-16", "100-16", "100-16", "100-16", "100-16"),
start_utc_day = c("2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01",
"2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01",
"2019-05-01", "2019-05-01"),
start = c("7:18:45", "7:27:30", "7:59:30", "8:37:30", "8:41:15",
"8:47:15", "8:52:45", "8:55:30", "8:57:45", "9:05:00"),
end = c("7:26:15", "7:59:15", "8:33:45", "8:40:30", "8:43:15",
"8:50:15", "8:55:15", "8:57:00", "9:00:00", "9:07:00")
)
# second dataset
location <- data.frame(stringsAsFactors=FALSE,
id = c("100-16", "100-16", "100-16", "100-16", "100-16",
"100-16", "100-16", "100-16", "100-16", "100-16"),
code = c("IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN",
"IN"),
utc_day = c("2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01",
"2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01",
"2019-05-01", "2019-05-01"),
start = c("7:13:30", "7:17:00", "7:22:00", "7:41:00", "8:14:15",
"8:33:45", "8:43:00", "9:08:45", "9:21:15", "9:32:00"),
end = c("7:15:30", "7:20:30", "7:31:00", "7:43:00", "8:15:45",
"8:35:15", "8:45:30", "9:12:15", "9:23:00", "9:35:15")
)
参考This link,我尝试使用interval_semi_join:
library(fuzzyjoin)
interval_semi_join(viewing,location, minoverlap = 3)
但出现此错误
Joining by: c("id", "start", "end")
index_match_fun(d1,d2)中的错误:
interval_join必须恰好在两列(开始和结束)上连接
答案 0 :(得分:1)
请考虑创建适当的日期/时间字段,然后运行模糊联接。字符列不能用于数字间隔比较或匹配。
viewing <- within(viewing, {
end_dt_time <- as.POSIXct(paste(start_utc_day, end), format="%Y-%m-%d %H:%M:%S")
start_dt_time <- as.POSIXct(paste(start_utc_day, start), format="%Y-%m-%d %H:%M:%S")
})
location <- within(location, {
end_dt_time <- as.POSIXct(paste(utc_day, end), format="%Y-%m-%d %H:%M:%S")
start_dt_time <- as.POSIXct(paste(utc_day, start), format="%Y-%m-%d %H:%M:%S")
})
interval_semi_join(viewing, location, by=c("start_dt_time", "end_dt_time"), minoverlap=3)