/usr/share/doc/r-cran-dplyr/tests/testthat/test-count-tally.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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | context("count-tally")
# count -------------------------------------------------------------------
test_that("can count variable called n", {
df <- data.frame(n = c(1, 1, 2, 2, 2))
out <- df %>% count(n)
expect_equal(names(out), c("n", "nn"))
expect_equal(out$nn, c(2, 3))
out <- df %>% count(n, sort = TRUE)
expect_equal(out$nn, c(3, 2))
})
test_that("count preserves grouping of input", {
df <- data.frame(g = c(1, 2, 2, 2))
out1 <- count(df, g)
expect_equal(group_vars(out1), character())
df2 <- df %>% group_by(g)
out2 <- count(df2)
expect_equal(group_vars(out2), "g")
})
test_that("grouped count includes group", {
df <- data.frame(g = c(1, 2, 2, 2))
res <- df %>% group_by(g) %>% count()
expect_equal(names(res), c("g", "n"))
expect_equal(res$n, c(1, 3))
expect_equal(group_vars(res), "g")
})
# add_count ---------------------------------------------------------------
test_that("can add counts of a variable called n", {
df <- data.frame(n = c(1, 1, 2, 2, 2))
out <- df %>% add_count(n)
expect_equal(names(out), c("n", "nn"))
expect_equal(out$n, df$n)
expect_equal(out$nn, c(2, 2, 3, 3, 3))
out <- df %>% add_count(n, sort = TRUE)
expect_equal(out$nn, c(3, 3, 3, 2, 2))
})
test_that("add_count respects and preserves existing groups", {
df <- data.frame(g = c(1, 2, 2, 2), val = c("b", "b", "b", "c"))
res <- df %>% add_count(val)
expect_equal(res$n, c(3, 3, 3, 1))
expect_no_groups(res)
res <- df %>% group_by(g) %>% add_count(val)
expect_equal(res$n, c(1, 2, 2, 1))
expect_groups(res, "g")
})
# tally -------------------------------------------------------------------
test_that("weighted tally drops NAs (#1145)", {
df <- data_frame(x = c(1, 1, NA))
expect_equal(tally(df, x)$n, 2)
})
# add_tally ---------------------------------------------------------------
test_that("can add tallies of a variable", {
df <- data.frame(a = c(1, 1, 2, 2, 2))
out <- df %>% group_by(a) %>% add_tally()
expect_equal(names(out), c("a", "n"))
expect_equal(out$a, df$a)
expect_equal(out$n, c(2, 2, 3, 3, 3))
out <- df %>% group_by(a) %>% add_tally(sort = TRUE)
expect_equal(out$n, c(3, 3, 3, 2, 2))
})
test_that("add_tally respects and preserves existing groups", {
df <- data.frame(g = c(1, 2, 2, 2), val = c("b", "b", "b", "c"))
res <- df %>% group_by(val) %>% add_tally()
expect_equal(res$n, c(3, 3, 3, 1))
expect_groups(res, "val")
res <- df %>% group_by(g, val) %>% add_tally()
expect_equal(res$n, c(1, 2, 2, 1))
expect_groups(res, c("g", "val"))
})
test_that("add_tally can be given a weighting variable", {
df <- data.frame(a = c(1, 1, 2, 2, 2), w = c(1, 1, 2, 3, 4))
out <- df %>% group_by(a) %>% add_tally(wt = w)
expect_equal(out$n, c(2, 2, 9, 9, 9))
out <- df %>% group_by(a) %>% add_tally(wt = w + 1)
expect_equal(out$n, c(4, 4, 12, 12, 12))
})
|