【发布时间】:2018-09-21 15:33:03
【问题描述】:
我需要从如下所示的数据表中找到最接近日期匹配的索引。
coldate: (data table or data frame)
mon_dd
1: 2018-09-04
2: 2018-09-10
3: 2018-09-17
4: 2018-09-24
5: 2018-10-01
6: 2018-10-08
7: 2018-10-15
8: 2018-10-22
9: 2018-10-29
x = as.Date("2018-09-25")
我使用以下代码,但它给了我一个错误,如下所示。
which.min(abs(x-coldate[,"mon_dd"]))
Error:
Error in x - coldate[, "mon_dd"] :
non-numeric argument to binary operator
有人可以帮我解决这个问题吗?
谢谢。
【问题讨论】:
-
which.min(abs(df$mon_dd - x))假设您的df$mon_dd属于Date类,如果不是先由df$mon_dd <- as.Date(df$mon_dd)转换它 -
谢谢。在我将它转换为日期类后它起作用了。