File: test-data_rotate.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 1,998 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
test_that("rotate data works as expected", {
  df <- mtcars[1:3, 1:4]

  expect_equal(
    data_rotate(df),
    structure(
      list(
        `Mazda RX4` = c(21, 6, 160, 110),
        `Mazda RX4 Wag` = c(21, 6, 160, 110),
        `Datsun 710` = c(22.8, 4, 108, 93)
      ),
      class = "data.frame",
      row.names = c("mpg", "cyl", "disp", "hp")
    )
  )

  expect_equal(
    data_rotate(df, rownames = "property"),
    structure(
      list(
        property = c("mpg", "cyl", "disp", "hp"),
        `Mazda RX4` = c(21, 6, 160, 110),
        `Mazda RX4 Wag` = c(21, 6, 160, 110),
        `Datsun 710` = c(22.8, 4, 108, 93)
      ),
      class = "data.frame",
      row.names = c(NA, 4L)
    )
  )

  expect_equal(
    data_rotate(df, colnames = TRUE),
    structure(
      list(
        `21` = c(6, 160, 110),
        `21` = c(6, 160, 110),
        `22.8` = c(4, 108, 93)
      ),
      class = "data.frame",
      row.names = c("cyl", "disp", "hp")
    )
  )

  expect_equal(
    data_rotate(df, rownames = "property", colnames = TRUE),
    structure(
      list(
        property = c("cyl", "disp", "hp"),
        `21` = c(6, 160, 110),
        `21` = c(6, 160, 110),
        `22.8` = c(4, 108, 93)
      ),
      class = "data.frame",
      row.names = c(NA, 3L)
    )
  )
})

test_that("data_rotate, arg 'colnames' works", {
  df <- mtcars[1:3, 1:4]
  df <- rownames_as_column(df)

  expected <- data.frame(
    `Mazda RX4` = c(21, 6, 160, 110),
    `Mazda RX4 Wag` = c(21, 6, 160, 110),
    `Datsun 710` = c(22.8, 4, 108, 93),
    check.names = FALSE
  )
  row.names(expected) <- c("mpg", "cyl", "disp", "hp")

  expect_identical(
    data_rotate(df, colnames = "rowname"),
    expected
  )
})

test_that("data_rotate warns if mixed types of data", {
  df <- mtcars[1:3, 1:4]
  df <- rownames_as_column(df)

  expect_warning(
    data_rotate(df),
    "mixed types of data"
  )

  df$rowname <- factor(df$rowname)
  expect_warning(
    data_rotate(df),
    "mixed types of data"
  )
})