Skip to content

Commit b74ef70

Browse files
committed
small fix
1 parent 61c29af commit b74ef70

4 files changed

Lines changed: 62 additions & 14 deletions

File tree

R/revisions.R

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ get_nth_release <- function(df, n = 0, diagonal = FALSE) {
23472347
dplyr::summarise(max_time = max(.data$time), .groups = "drop"),
23482348
by = c("id", "min_pub_date" = "pub_date")
23492349
) %>%
2350-
dplyr::select(.data$id, .data$max_time)
2350+
dplyr::select("id", "max_time")
23512351

23522352
nth_release <- nth_release %>%
23532353
dplyr::left_join(diagonal_thresholds, by = "id") %>%
@@ -2456,10 +2456,28 @@ get_first_release <- function(df, diagonal = FALSE) {
24562456
}
24572457

24582458
if (diagonal) {
2459-
df <- df %>%
2460-
dplyr::filter(
2461-
!dplyr::lead(.data$pub_date) == (.data$pub_date)
2462-
)
2459+
if ("id" %in% colnames(df)) {
2460+
diagonal_thresholds <- df %>%
2461+
dplyr::group_by(.data$id) %>%
2462+
dplyr::summarise(min_pub_date = min(.data$pub_date), .groups = "drop") %>%
2463+
dplyr::left_join(
2464+
df %>%
2465+
dplyr::group_by(.data$id, .data$pub_date) %>%
2466+
dplyr::summarise(max_time = max(.data$time), .groups = "drop"),
2467+
by = c("id", "min_pub_date" = "pub_date")
2468+
) %>%
2469+
dplyr::select("id", "max_time")
2470+
2471+
df <- df %>%
2472+
dplyr::left_join(diagonal_thresholds, by = "id") %>%
2473+
dplyr::filter(.data$time >= .data$max_time) %>%
2474+
dplyr::select(-"max_time")
2475+
} else {
2476+
min_pub_date <- min(df$pub_date)
2477+
max_time <- max(df$time[df$pub_date == min_pub_date])
2478+
df <- df %>%
2479+
dplyr::filter(.data$time >= max_time)
2480+
}
24632481
}
24642482

24652483
# Add the class only if it is not already present

R/utils.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ vintages_long <- function(df, names_to = "pub_date", keep_na = FALSE) {
7171
check <- vintages_check(df[[id]])
7272
if (check == "long") {
7373
rlang::warn("The input data is already in long format.")
74-
return(df[[id]])
74+
long_df_tmp <- df[[id]] %>%
75+
dplyr::mutate(id = .env$id)
76+
} else {
77+
long_df_tmp <- df[[id]] %>%
78+
tidyr::pivot_longer(
79+
cols = -"time",
80+
names_to = names_to,
81+
values_to = "value"
82+
) %>%
83+
dplyr::mutate(id = .env$id)
7584
}
76-
long_df_tmp <- df[[id]] %>%
77-
tidyr::pivot_longer(
78-
cols = -"time",
79-
names_to = names_to,
80-
values_to = "value"
81-
) %>%
82-
dplyr::mutate(id = id)
8385

8486
if (keep_na) {
8587
return(long_df_tmp)

tests/testthat/test-revisions.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,21 @@ test_that("get_first_release returns first release", {
379379
expect_true(all(result$release == "release_0"))
380380
})
381381

382+
test_that("get_first_release diagonal keeps the real first-release diagonal", {
383+
df_single_wide <- data.frame(
384+
time = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01")),
385+
`2020-03-01` = c(1, 2, 3),
386+
check.names = FALSE
387+
)
388+
389+
result <- get_first_release(vintages_long(df_single_wide), diagonal = TRUE)
390+
391+
expect_equal(nrow(result), 1)
392+
expect_equal(result$time, as.Date("2020-03-01"))
393+
expect_equal(result$pub_date, as.Date("2020-03-01"))
394+
expect_true(all(result$release == "release_0"))
395+
})
396+
382397
# ===== Tests for get_latest_release =====
383398

384399
test_that("get_latest_release returns latest release", {

tests/testthat/test-utils.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ test_that("vintages_long handles list input", {
9696
expect_equal(nrow(result), 48) # 12 * 2 vintages * 2 ids
9797
})
9898

99+
test_that("vintages_long keeps ids for long-format list input", {
100+
long_list <- list(US = df_long, EA = df_long)
101+
102+
expect_warning(
103+
result <- vintages_long(long_list, names_to = "pub_date"),
104+
"already in long format"
105+
)
106+
107+
expect_true("id" %in% colnames(result))
108+
expect_equal(sort(unique(result$id)), c("EA", "US"))
109+
expect_equal(unname(as.integer(table(result$id))), c(nrow(df_long), nrow(df_long)))
110+
})
111+
99112
test_that("vintages_long validates names_to parameter", {
100113
expect_error(
101114
vintages_long(df_wide, names_to = "invalid"),
@@ -591,4 +604,4 @@ test_that("conversion handles tibbles", {
591604

592605
long_again <- vintages_long(wide, names_to = "pub_date")
593606
expect_s3_class(long_again, "tbl_df")
594-
})
607+
})

0 commit comments

Comments
 (0)