我有2个像这样的数据框
ID <- c("A","A")
StartDatetime <- c("2015-09-29 00:00:13", "2015-09-29 05:55:50")
EndDatetime <- c("2015-09-29 00:13:56", "2015-09-29 06:13:50")
Measurement <- c("Length","Length")
df1 <- data.frame(ID,StartDatetime,EndDatetime,Measurement)
ID <- c("A","A")
MStart <- c("09/29/2015 00:02:13", "09/29/2015 05:56:50")
MEnd <- c("09/29/2015 00:11:12", "09/29/2015 06:55:50")
Measurement <- c("Length","Length")
df2 <- data.frame(ID,MStart,MEnd,Measurement)
在SO的用户帮助下,我能够像这样格式化日期时间列
library(dplyr)
library(lubridate)
df1$StartDatetime <- ymd_hms(df1$StartDatetime)
df1$EndDatetime <- ymd_hms(df1$EndDatetime)
df2$MStart<- mdy_hms(df2$MStart)
df2$MEnd<- mdy_hms(df2$MEnd)
我正在尝试使用
合并这两个数据帧df <- merge(df1,df2,by = c("ID","Measurement"))
问题是它返回4行
ID Measurement StartDatetime EndDatetime MStart MEnd
1 A Length 2015-09-29 00:00:13 2015-09-29 00:13:56 2015-09-29 00:02:13 2015-09-29 00:11:12
2 A Length 2015-09-29 00:00:13 2015-09-29 00:13:56 2015-09-29 05:56:50 2015-09-29 06:55:50
3 A Length 2015-09-29 05:55:50 2015-09-29 06:13:50 2015-09-29 00:02:13 2015-09-29 00:11:12
4 A Length 2015-09-29 05:55:50 2015-09-29 06:13:50 2015-09-29 05:56:50 2015-09-29 06:55:50
我只想看第1行和第1行。 4因为我的MStart应该在StartDatetime&amp; EndDatetime因此需要删除2,3行。
我想要的输出是
ID Measurement StartDatetime EndDatetime MStart MEnd
1 A Length 2015-09-29 00:00:13 2015-09-29 00:13:56 2015-09-29 00:02:13 2015-09-29 00:11:12
2 A Length 2015-09-29 05:55:50 2015-09-29 06:13:50 2015-09-29 05:56:50 2015-09-29 06:55:50
我该如何过滤它?请提供您的建议。
编辑 - 我试过eddi建议的foverlaps并抛出错误。
colnames(df2)[2] <- "StartDatetime"
colnames(df2)[3] <- "EndDatetime"
y1 = data.table(df1)
x1 = data.table(df2)
setkey(y1, StartDatetime, EndDatetime)
foverlaps(x, y, type="within") ## matches iff 'x' is within 'y'
我收到错误
Error in setkeyv(x, cols, verbose = verbose, physical = physical) :
4 arguments passed to .Internal(nchar) which requires 3