Reputation: 363
I have this dataframe
dt <-
data.frame(
date = c(as.Date("2022-03-01"), as.Date("2022-03-02"), as.Date("2022-03-08"), as.Date("2022-03-08"), as.Date("2022-04-01"), as.Date("2022-04-10"))
)
My goal is to create new column in dplyr and assign 1/0 depending on date difftime in days. In case that difference is lower than 7, 0 schould be assigned and vice versa. Thus desired result is.
result <-
data.frame(
date = c(as.Date("2022-03-01"), as.Date("2022-03-02"), as.Date("2022-03-08"), as.Date("2022-03-08"), as.Date("2022-04-01"), as.Date("2022-04-10")),
cache = c(0, 1, 0, 1, 0, 0)
)
Upvotes: 1
Views: 139
Reputation: 76402
Here is a way with diff
. It works because objects of class "Date"
record the integer number of days since an origin and are internally integers.
suppressPackageStartupMessages(library(dplyr))
dt <-
data.frame(
date = c(as.Date("2022-03-01"), as.Date("2022-03-02"), as.Date("2022-03-08"), as.Date("2022-03-08"), as.Date("2022-04-01"), as.Date("2022-04-10"))
)
dt %>%
mutate(cache = c(0, diff(date) >= 7L))
#> date cache
#> 1 2022-03-01 0
#> 2 2022-03-02 0
#> 3 2022-03-08 0
#> 4 2022-03-08 0
#> 5 2022-04-01 1
#> 6 2022-04-10 1
Created on 2022-07-22 by the reprex package (v2.0.1)
Following the exchange of comments, the code below seems to be the right one.
suppressPackageStartupMessages(library(dplyr))
dt <-
data.frame(
date = c(as.Date("2022-03-01"), as.Date("2022-03-02"), as.Date("2022-03-08"), as.Date("2022-03-08"), as.Date("2022-04-01"), as.Date("2022-04-10"))
)
dt %>%
mutate(
cache = as.integer(date - first(date) >= 7L)
)
#> date cache
#> 1 2022-03-01 0
#> 2 2022-03-02 0
#> 3 2022-03-08 1
#> 4 2022-03-08 1
#> 5 2022-04-01 1
#> 6 2022-04-10 1
Created on 2022-07-22 by the reprex package (v2.0.1)
In the creation of the dates vector, it would have been simpler to call as.Date
only once.
as.Date(c("2022-03-01", "2022-03-02", "2022-03-08", "2022-03-08", "2022-04-01", "2022-04-10"))
Upvotes: 1