Here is a case_when
approach. First of all we need to get a day of the year values for our period. The start of astronomical summer for northern hemisphere (21st of June) is 172rd day and end is 267th day (23rd of September). You can do it with lubridate::yday("2019-06-21")
.
Then we need do the same for our dataframe. So we got yours ts1
. We need to transform it into data.frame
or tibble
and calculate yday
:
library(lubridate)
library(dplyr)
ts1 <- seq(ymd('2016-01-01'), ymd('2018-12-31'), '1 day')
ts1 <- tibble(date = (ts1),
day = yday(ts1))
Using sqldf
library(sqldf)
sqldf("select ts1.*, case when (ts1.day >= 172 and ts1.day <= 267)
then 1 else 0 end as TOY
from ts1", method = c("Date", "numeric", "logical")) %>%
as_tibble()
# A tibble: 1,096 x 3
date day TOY
<date> <dbl> <lgl>
1 2016-01-01 1 FALSE
2 2016-01-02 2 FALSE
3 2016-01-03 3 FALSE
4 2016-01-04 4 FALSE
5 2016-01-05 5 FALSE
6 2016-01-06 6 FALSE
7 2016-01-07 7 FALSE
8 2016-01-08 8 FALSE
9 2016-01-09 9 FALSE
10 2016-01-10 10 FALSE
# ... with 1,086 more rows
Using dplyr
ts1 %>%
mutate(TOY = case_when(day >= 172 & day <= 267 ~ "summer",
TRUE ~ "other"))
# A tibble: 1,096 x 3
date day TOY
<date> <dbl> <chr>
1 2016-01-01 1 other
2 2016-01-02 2 other
3 2016-01-03 3 other
4 2016-01-04 4 other
5 2016-01-05 5 other
6 2016-01-06 6 other
7 2016-01-07 7 other
8 2016-01-08 8 other
9 2016-01-09 9 other
10 2016-01-10 10 other
# ... with 1,086 more rows