File: test-reshape.R

package info (click to toggle)
r-cran-skimr 2.1.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,188 kB
  • sloc: sh: 13; makefile: 2
file content (133 lines) | stat: -rw-r--r-- 4,347 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
test_that("You can parition a skim_df", {
  skimmed <- skim(iris)
  input <- partition(skimmed)
  expect_s3_class(input, "skim_list")
  expect_length(input, 2)
  expect_named(input, c("factor", "numeric"))
  attrs <- attributes(input)
  expect_equal(attrs$data_rows, 150)
  expect_equal(attrs$data_cols, 5)
  expect_identical(attrs$df_name, "`iris`")
  expect_identical(attrs$base_skimmers, c("n_missing", "complete_rate"))
  expect_identical(
    attrs$skimmers_used,
    list(
      numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist"),
      factor = c("ordered", "n_unique", "top_counts")
    )
  )

  # Subtables
  expect_s3_class(input$factor, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
  expect_n_rows(input$factor, 1)
  expect_n_columns(input$factor, 6)
  expect_named(input$factor, c(
    "skim_variable", "n_missing", "complete_rate", "ordered", "n_unique",
    "top_counts"
  ))

  expect_s3_class(input$numeric, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
  expect_n_rows(input$numeric, 4)
  expect_n_columns(input$numeric, 11)
  expect_named(input$numeric, c(
    "skim_variable", "n_missing", "complete_rate", "mean",
    "sd", "p0", "p25", "p50", "p75", "p100",
    "hist"
  ))
})

test_that("Partitioning works in a round trip", {
  skimmed <- skim(iris)
  partitioned <- partition(skimmed)
  input <- bind(partitioned)
  expect_identical(input, skimmed)
})

test_that("You can yank a subtable from a skim_df", {
  skimmed <- skim(iris)
  input <- yank(skimmed, "numeric")
  expect_s3_class(input, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
  expect_n_rows(input, 4)
  expect_n_columns(input, 11)
  expect_named(input, c(
    "skim_variable", "n_missing", "complete_rate", "mean",
    "sd", "p0", "p25", "p50", "p75", "p100",
    "hist"
  ))
})

test_that("Partition is safe if some skimmers are missing", {
  skimmed <- skim(iris)
  reduced <- dplyr::select(skimmed, skim_variable, skim_type, n_missing)
  partitioned <- partition(reduced)
  expect_length(partitioned, 2)
  expect_named(partitioned, c("factor", "numeric"))
  expect_named(partitioned$numeric, c("skim_variable", "n_missing"))
})

test_that("Partition handles new columns", {
  skimmed <- skim(iris)
  expanded <- dplyr::mutate(
    skimmed,
    mean2 = numeric.mean^2,
    complete2 = complete_rate^2
  )
  partitioned <- partition(expanded)
  expect_named(partitioned$numeric, c(
    "skim_variable", "n_missing", "complete_rate", "mean",
    "sd", "p0", "p25", "p50", "p75", "p100",
    "hist", "mean2", "complete2"
  ))
})

test_that("focus() matches select(data, skim_type, skim_variable, ...)", {
  skimmed <- skim(iris)
  expected <- dplyr::select(
    skimmed, skim_type, skim_variable, n_missing
  )
  expect_equal(focus(skimmed, n_missing), expected, check.attributes = FALSE)
})

test_that("focus() does not allow dropping skim metadata columns", {
  skimmed <- skim(iris)
  expect_error(focus(skimmed, -skim_variable), "Cannot drop")
  expect_error(focus(skimmed, -skim_type), "Cannot drop")
})

test_that("skim_to_wide() returns a deprecation warning", {
  expect_warning(skim_to_wide(iris))
})

test_that("skim_to_list() returns a deprecation warning", {
  expect_warning(skim_to_list(iris))
})

test_that("to_long() returns a long tidy data frame with 4 columns", {
  skimmed_long <- to_long(iris)
  # Statistics from the skim_df  with values of NA are not included
  expect_n_rows(skimmed_long, 45)
  expect_equal(
    names(skimmed_long),
    c("skim_type", "skim_variable", "stat", "formatted")
  )
  expect_equal(length(unique(skimmed_long$stat)), 13)
  expect_equal(length(unique(skimmed_long$skim_type)), 2)
  expect_equal(length(unique(skimmed_long$skim_variable)), 5)
})

test_that("to_long() on a skim_df returns a long tidy df with 4 columns", {
  skimmed_long <- to_long(skim(iris))
  # Statistics from the skim_df  with values of NA are not included
  expect_n_rows(skimmed_long, 45)
  expect_equal(
    names(skimmed_long),
    c("skim_type", "skim_variable", "stat", "formatted")
  )
  expect_equal(length(unique(skimmed_long$stat)), 13)
  expect_equal(length(unique(skimmed_long$skim_type)), 2)
  expect_equal(length(unique(skimmed_long$skim_variable)), 5)
})

test_that("to_long() on a df and a skim_df from same df are identical", {
  expect_identical(to_long(skim(chickwts)), to_long(chickwts))
})