d1
在某些地点进行观察
l
和时代
t
.
> head(d1, 3)
id l p t X
1 1 258 2016 2016-01-05 -1.158644
2 5 261 2016 2016-01-14 1.604873
3 2 261 2016 2016-01-20 -1.102002
在另一个数据帧中
p2
我有时间间隔
t1:t2
地点
L
,我想逐行检查
d1
匹配的位置和时间间隔元组
p2
> head(p2, 3)
l p t1 t2
1 261 2016 2016-01-11 2016-01-25
2 261 2017 2017-02-27 2017-03-13
3 261 2017 2017-03-01 2017-03-15
在正的情况下,虚拟变量
d1$match
应给出值1,在负值情况下为0:
# [1] 0 1 1 ...
L
p
将两个数据帧转换为字符串并进行比较,然后检查
在于
t1:t2
.
p1
. 此外,还发出了警告,因为车辆似乎存在问题
"Date"
上课。
> p1
l p t1 t2
1 261 2016 2016-01-11 2016-01-25
2 261 2017 2017-02-27 2017-03-13
4 258 2018 2018-01-09 2018-01-23
p <- p1
p.strg <- sapply(1:nrow(p), function(x) {
do.call(paste, c(p[x, c("l", "p")], sep = "|"))
})
sapply(1:nrow(d1), function(x) {
strg <- do.call(paste, c(d1[x, c("l", "p")], sep = "|"))
t.d <- d1[x, "t"]
t.p <- p[which(p.strg %in% strg), c("t1", "t2")]
return(as.integer(any(p.strg %in% strg) & t.d >= t.p[1] &
t.d <= t.p[2]))
})
# [1] 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0
# There were 30 warnings (use warnings() to see them)
# warnings()
# Warning messages:
# 1: In FUN(X[[i]], ...) :
# Incompatible methods ("Ops.Date", "Ops.data.frame") for ">="
# ...
如果周期确实如中所示重叠
p2
p <- p2
p.strg <- sapply(1:nrow(p), function(x) {
do.call(paste, c(p[x, c("l", "p")], sep = "|"))
})
sapply(1:nrow(d1), function(x) {
strg <- do.call(paste, c(d1[x, c("l", "p")], sep = "|"))
t.d <- d1[x, "t"]
t.p <- p[which(p.strg %in% strg), c("t1", "t2")]
return(as.integer(any(p.strg %in% strg) & t.d >= t.p[1] &
t.d <= t.p[2]))
})
这根本不起作用:
Error in FUN(X[[i]], ...) :
(list) object cannot be coerced to type 'double'
In addition: There were 13 warnings (use warnings() to see them)
我想我有点迷路了。解决这一问题的更好办法是什么
?
注:
我的原始数据比较广泛(d1:20000 x 11,p2:1700 x 8),所以我需要一个高效的解决方案。
d1 <- structure(list(id = c(1L, 5L, 2L, 3L, 1L, 3L, 4L, 5L, 2L, 3L,
5L, 1L, 2L, 4L, 4L), l = c(258, 261, 261, 260, 258, 260, 261,
261, 259, 260, 261, 258, 259, 261, 261), p = c(2016, 2016, 2016,
2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018,
2018), t = structure(c(16805, 16814, 16820, 16924, 17193, 17211,
17227, 17229, 17348, 17481, 17517, 17543, 17554, 17787, 17887
), class = "Date"), X = c(-1.15864442153663, 1.60487335898257,
-1.10200153102672, -0.823719007033067, 1.20944271845298, 0.790388149166713,
-1.0996495357495, -0.421449225963478, -0.243567712934607, -0.337415580767635,
-1.64590022554026, 2.11206142393207, -0.950235138478342, -2.08164602167738,
-1.88576409729638), match = c(0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L)), row.names = c(NA, -15L), class = "data.frame")
p1 <- structure(list(l = c(261, 261, 258), p = c(2016, 2017, 2018),
t1 = structure(c(16811, 17224, 17540), class = "Date"), t2 = structure(c(16825,
17238, 17554), class = "Date")), row.names = c(1L, 2L, 4L
), class = "data.frame")
p2 <- structure(list(l = c(261, 261, 261, 258, 259, 261), p = c(2016,
2017, 2017, 2018, 2018, 2018), t1 = structure(c(16811, 17224,
17226, 17540, 17551, 17884), class = "Date"), t2 = structure(c(16825,
17238, 17240, 17554, 17565, 17898), class = "Date")), row.names = c(NA,
-6L), class = "data.frame")