这里有一种方法。首先,编写一个函数,检查特定日期
x
在另外两个日期之间
d1
和
d2
.
check <- function(x, d1, d2) ifelse(x >= d1 & x <= d2, 1, 0)
然后加载tidyverse,并使用
purrr::map
,将名称设置为日期,然后将所有列绑定在一起。
library(tidyverse)
df_checked <- map(DatesChecked, ~check(., d$StartDate, d$EndDate)) %>%
set_names(DatesChecked) %>%
bind_cols()
# Show first five columns
df_checked[ ,1:5]
# A tibble: 6 x 5
`2012-06-30` `2012-07-01` `2012-07-02` `2012-07-03` `2012-07-04`
<dbl> <dbl> <dbl> <dbl> <dbl>
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
6 0 0 0 0 0
# Show specific column mentioned in question
df_checked["2014-01-01"]
# A tibble: 6 x 1
`2014-01-01`
<dbl>
1 1.00
2 0
3 0
4 0
5 0
6 0