我在R中有一个棘手的问题,我似乎无法在不诉诸循环的情况下解决。
我从timeDates的向量开始:
library(timeDate)
dates <- timeDate(c("2014-01-01","2008-01-02","2008-01-03","2008-01-04"))
我想在预设的黑名单中找到任何日期的索引:
dateBlacklist <- timeDate(c("2008-01-02","2008-01-03"))
结果如下:
indexesOfBlacklistedDates <- c(2,3)
答案 0 :(得分:2)
丑陋的解决方案:
indexesOfBlacklistedDates <- which(timeDate:::as.character.timeDate(dates) %in% timeDate:::as.character.timeDate(dateBlacklist))
另一个不那么丑陋的解决方案(类似于@ agstudy&#39;答案)
which(as.character(dates) %in% as.character(dateBlacklist))
答案 1 :(得分:2)
优雅的解决方案:)
match(as.character(dateBlacklist), as.character(dates))
[1] 2 3