/usr/share/doc/r-cran-dplyr/tests/testthat/test-lead-lag.R is in r-cran-dplyr 0.7.4-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | context("Lead and lag")
test_that("lead and lag preserve factors", {
x <- factor(c("a", "b", "c"))
expect_equal(levels(lead(x)), c("a", "b", "c"))
expect_equal(levels(lag(x)), c("a", "b", "c"))
})
test_that("lead and lag preserves dates and times", {
x <- as.Date("2013-01-01") + 1:3
y <- as.POSIXct(x)
expect_is(lead(x), "Date")
expect_is(lag(x), "Date")
expect_is(lead(y), "POSIXct")
expect_is(lag(y), "POSIXct")
})
test_that("#925 is fixed", {
data <- data_frame(
name = c("Rob", "Pete", "Rob", "John", "Rob", "Pete", "John", "Pete", "John", "Pete", "Rob", "Rob"),
time = c(3, 2, 5, 3, 2, 3, 2, 4, 1, 1, 4, 1)
)
res <- data %>% group_by(name) %>% mutate(lag_time = lag(time))
expect_equal(
res$lag_time[res$name == "Rob"],
c(NA, head(data$time[data$name == "Rob"], -1))
)
expect_equal(
res$lag_time[res$name == "Pete"],
c(NA, head(data$time[data$name == "Pete"], -1))
)
expect_equal(
res$lag_time[res$name == "John"],
c(NA, head(data$time[data$name == "John"], -1))
)
})
test_that("#937 is fixed", {
df <- data_frame(
name = rep(c("Al", "Jen"), 3),
score = rep(c(100, 80, 60), 2)
)
res <- df %>% group_by(name) %>% mutate(next.score = lead(score))
expect_equal(
res$next.score[res$name == "Al"],
c(tail(df$score[df$name == "Al"], -1), NA)
)
expect_equal(
res$next.score[res$name == "Jen"],
c(tail(df$score[df$name == "Jen"], -1), NA)
)
})
test_that("input checks", {
expect_error(
lead(letters, -1),
"`n` must be a nonnegative integer scalar, not double of length 1",
fixed = TRUE
)
expect_error(
lead(letters, "1"),
"`n` must be a nonnegative integer scalar, not string of length 1",
fixed = TRUE
)
expect_error(
lag(letters, -1),
"`n` must be a nonnegative integer scalar, not double of length 1",
fixed = TRUE
)
expect_error(
lag(letters, "1"),
"`n` must be a nonnegative integer scalar, not string of length 1",
fixed = TRUE
)
})
|