/tests/testthat/test-pivot-wide.R

https://github.com/cran/tidyr · R · 217 lines · 169 code · 41 blank · 7 comment · 0 complexity · beeae408d24e6561e4422b7c1c7ba24c MD5 · raw file

  1. context("test-pivot-wide")
  2. test_that("can pivot all cols to wide", {
  3. df <- tibble(key = c("x", "y", "z"), val = 1:3)
  4. pv <- pivot_wider(df, names_from = key, values_from = val)
  5. expect_named(pv, c("x", "y", "z"))
  6. expect_equal(nrow(pv), 1)
  7. })
  8. test_that("non-pivoted cols are preserved", {
  9. df <- tibble(a = 1, key = c("x", "y"), val = 1:2)
  10. pv <- pivot_wider(df, names_from = key, values_from = val)
  11. expect_named(pv, c("a", "x", "y"))
  12. expect_equal(nrow(pv), 1)
  13. })
  14. test_that("implicit missings turn into explicit missings", {
  15. df <- tibble(a = 1:2, key = c("x", "y"), val = 1:2)
  16. pv <- pivot_wider(df, names_from = key, values_from = val)
  17. expect_equal(pv$a, c(1, 2))
  18. expect_equal(pv$x, c(1, NA))
  19. expect_equal(pv$y, c(NA, 2))
  20. })
  21. test_that("error when overwriting existing column", {
  22. df <- tibble(
  23. a = c(1, 1),
  24. key = c("a", "b"),
  25. val = c(1, 2)
  26. )
  27. expect_error(
  28. pivot_wider(df, names_from = key, values_from = val),
  29. "bad names"
  30. )
  31. })
  32. test_that("grouping is preserved", {
  33. df <- tibble(g = 1, k = "x", v = 2)
  34. out <- df %>%
  35. dplyr::group_by(g) %>%
  36. pivot_wider(names_from = k, values_from = v)
  37. expect_equal(dplyr::group_vars(out), "g")
  38. })
  39. # https://github.com/tidyverse/tidyr/issues/804
  40. test_that("column with `...j` name can be used as `names_from`", {
  41. df <- tibble(...8 = c("x", "y", "z"), val = 1:3)
  42. pv <- pivot_wider(df, names_from = ...8, values_from = val)
  43. expect_named(pv, c("x", "y", "z"))
  44. expect_equal(nrow(pv), 1)
  45. })
  46. test_that("data frame columns pivot correctly", {
  47. df <- tibble(
  48. i = c(1, 2, 1, 2),
  49. g = c("a", "a", "b", "b"),
  50. d = tibble(x = 1:4, y = 5:8)
  51. )
  52. out <- pivot_wider(df, names_from = g, values_from = d)
  53. expect_equal(out$a$x, 1:2)
  54. expect_equal(out$b$y, 7:8)
  55. })
  56. # column names -------------------------------------------------------------
  57. test_that("names_glue affects output names", {
  58. df <- tibble(
  59. x = c("X", "Y"),
  60. y = 1:2,
  61. a = 1:2,
  62. b = 1:2
  63. )
  64. spec <- build_wider_spec(df, x:y, a:b, names_glue = '{x}{y}_{.value}')
  65. expect_equal(spec$.name, c("X1_a", "Y2_a", "X1_b", "Y2_b"))
  66. })
  67. test_that("can sort column names", {
  68. df <- tibble(
  69. int = c(1, 3, 2),
  70. fac = factor(int, levels = 1:3, labels = c("Mon", "Tue", "Wed")),
  71. )
  72. spec <- build_wider_spec(df,
  73. names_from = fac,
  74. values_from = int,
  75. names_sort = TRUE
  76. )
  77. expect_equal(spec$.name, levels(df$fac))
  78. })
  79. # keys ---------------------------------------------------------
  80. test_that("can override default keys", {
  81. df <- tribble(
  82. ~row, ~name, ~var, ~value,
  83. 1, "Sam", "age", 10,
  84. 2, "Sam", "height", 1.5,
  85. 3, "Bob", "age", 20,
  86. )
  87. pv <- df %>% pivot_wider(id_cols = name, names_from = var, values_from = value)
  88. expect_equal(nrow(pv), 2)
  89. })
  90. # non-unqiue keys ---------------------------------------------------------
  91. test_that("duplicated keys produce list column with warning", {
  92. df <- tibble(a = c(1, 1, 2), key = c("x", "x", "x"), val = 1:3)
  93. expect_warning(
  94. pv <- pivot_wider(df, names_from = key, values_from = val),
  95. "list-col"
  96. )
  97. expect_equal(pv$a, c(1, 2))
  98. expect_equal(as.list(pv$x), list(c(1L, 2L), 3L))
  99. })
  100. test_that("warning suppressed by supplying values_fn", {
  101. df <- tibble(a = c(1, 1, 2), key = c("x", "x", "x"), val = 1:3)
  102. expect_warning(
  103. pv <- pivot_wider(df,
  104. names_from = key,
  105. values_from = val,
  106. values_fn = list(val = list)
  107. ),
  108. NA
  109. )
  110. expect_equal(pv$a, c(1, 2))
  111. expect_equal(as.list(pv$x), list(c(1L, 2L), 3L))
  112. })
  113. test_that("values_fn can be a single function", {
  114. df <- tibble(a = c(1, 1, 2), key = c("x", "x", "x"), val = c(1, 10, 100))
  115. pv <- pivot_wider(df, names_from = key, values_from = val, values_fn = sum)
  116. expect_equal(pv$x, c(11, 100))
  117. })
  118. test_that("values_summarize applied even when no-duplicates", {
  119. df <- tibble(a = c(1, 2), key = c("x", "x"), val = 1:2)
  120. pv <- pivot_wider(df,
  121. names_from = key,
  122. values_from = val,
  123. values_fn = list(val = list)
  124. )
  125. expect_equal(pv$a, c(1, 2))
  126. expect_equal(as.list(pv$x), list(1L, 2L))
  127. })
  128. # can fill missing cells --------------------------------------------------
  129. test_that("can fill in missing cells", {
  130. df <- tibble(g = c(1, 2), var = c("x", "y"), val = c(1, 2))
  131. widen <- function(...) {
  132. df %>% pivot_wider(names_from = var, values_from = val, ...)
  133. }
  134. expect_equal(widen()$x, c(1, NA))
  135. expect_equal(widen(values_fill = 0)$x, c(1, 0))
  136. expect_equal(widen(values_fill = list(val = 0))$x, c(1, 0))
  137. })
  138. test_that("values_fill only affects missing cells", {
  139. df <- tibble(g = c(1, 2), names = c("x", "y"), value = c(1, NA))
  140. out <- pivot_wider(df, names_from = names, values_from = value, values_fill = 0 )
  141. expect_equal(out$y, c(0, NA))
  142. })
  143. # multiple values ----------------------------------------------------------
  144. test_that("can pivot from multiple measure cols", {
  145. df <- tibble(row = 1, var = c("x", "y"), a = 1:2, b = 3:4)
  146. sp <- build_wider_spec(df, names_from = var, values_from = c(a, b))
  147. pv <- pivot_wider_spec(df, sp)
  148. expect_named(pv, c("row", "a_x", "a_y", "b_x", "b_y"))
  149. expect_equal(pv$a_x, 1)
  150. expect_equal(pv$b_y, 4)
  151. })
  152. test_that("can pivot from multiple measure cols using all keys", {
  153. df <- tibble(var = c("x", "y"), a = 1:2, b = 3:4)
  154. sp <- build_wider_spec(df, names_from = var, values_from = c(a, b))
  155. pv <- pivot_wider_spec(df, sp)
  156. expect_named(pv, c("a_x", "a_y", "b_x", "b_y"))
  157. expect_equal(pv$a_x, 1)
  158. expect_equal(pv$b_y, 4)
  159. })
  160. test_that("column order in output matches spec", {
  161. df <- tribble(
  162. ~hw, ~name, ~mark, ~pr,
  163. "hw1", "anna", 95, "ok",
  164. "hw2", "anna", 70, "meh",
  165. )
  166. # deliberately create weird order
  167. sp <- tribble(
  168. ~hw, ~.value, ~.name,
  169. "hw1", "mark", "hw1_mark",
  170. "hw1", "pr", "hw1_pr",
  171. "hw2", "pr", "hw2_pr",
  172. "hw2", "mark", "hw2_mark",
  173. )
  174. pv <- pivot_wider_spec(df, sp)
  175. expect_named(pv, c("name", sp$.name))
  176. })