Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/genhol first year #286

Merged
merged 2 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
- seas() works within forking parallization (e.g., mclapply) #276
Thanks to @jonlachmann!
- annual series are read correctly #264
- January first can be used in genhol() #261
- Jan 1 can be used in genhol() #261
- genhol() produces correct result in 1931 #193


1.8.4.2 (unreleased)
Expand Down
13 changes: 8 additions & 5 deletions R/genhol.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ genhol <- function(x, start = 0, end = 0, frequency = 12, center = "none"){

# "ts" object with each date in the right period
event.st.ts <- z.ts
event.st.ts[cut(event.st.added, by, labels = F)] <- as.character(event.st)
event.st.ts[cut(event.st.added, by, labels = FALSE)] <- as.character(event.st)

event.en.ts <- z.ts
event.en.ts[cut(event.en.added, by, labels = F)] <- as.character(event.en)
event.en.ts[cut(event.en.added, by, labels = FALSE)] <- as.character(event.en)

# number of days
days <- pmin((period.en), as.Date(as.character(event.en.ts)), na.rm = T) -
pmax(period.st, as.Date(as.character(event.st.ts)), na.rm = T) + 1
days <- pmin((period.en), as.Date(as.character(event.en.ts)), na.rm = TRUE) -
pmax(period.st, as.Date(as.character(event.st.ts)), na.rm = TRUE) + 1

# filling NAs with start and end dates
fillNA <- function(x){
Expand All @@ -240,8 +240,11 @@ genhol <- function(x, start = 0, end = 0, frequency = 12, center = "none"){
# drop
drop <- is.na(event.st.ts) & is.na(event.en.ts)

fill.na.event.en.ts <- fillNA(event.en.ts)
fill.na.event.en.ts[is.na(fill.na.event.en.ts)] <- as.Date("0001-01-01")

# dont drop these (start value larger than end value)
drop[fillNA(event.st.ts) > fillNA(event.en.ts)] <- FALSE
drop[fillNA(event.st.ts) > fill.na.event.en.ts] <- FALSE

days[drop] <- 0
z.raw <- ts(c(days), start = start(z.ts), frequency = frequency(z.ts))
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ test_that("January first can be used in genhol() #261", {
})





test_that("genhol() produces correct result in 1931 #193", {
ans <- genhol(easter, start = -46, end = 0, frequency = 12)
expect_equal(sum(abs(rowSums(matrix(ans, ncol = 12, byrow = TRUE)) - 1)), 0)
})