/tests/testthat/test-tidyr.R

https://github.com/tidyverts/tsibble · R · 146 lines · 133 code · 13 blank · 0 comment · 0 complexity · 1958115029e42c834ae1265e95b642ca MD5 · raw file

  1. library(tidyr)
  2. tsbl <- tsibble(
  3. qtr = rep(yearquarter("2010 Q1") + 0:9, 3),
  4. group = rep(c("x", "y", "z"), each = 10),
  5. value = rnorm(30),
  6. key = group, index = qtr
  7. )
  8. pedestrian <- pedestrian %>%
  9. group_by(Sensor) %>%
  10. slice(1:10) %>%
  11. ungroup()
  12. tourism <- tourism %>%
  13. group_by_key() %>%
  14. slice(1:10) %>%
  15. ungroup()
  16. test_that("pivot_wider()", {
  17. out <- tsbl %>%
  18. pivot_wider(names_from = group, values_from = value)
  19. expect_is(out, "tbl_ts")
  20. expect_equal(key(out), list())
  21. expect_named(out, c("qtr", "x", "y", "z"))
  22. out_grp <- tsbl %>%
  23. group_by(group) %>%
  24. pivot_wider(names_from = group, values_from = value)
  25. expect_equal(groups(out_grp), list())
  26. out2 <- tourism %>%
  27. pivot_wider(names_from = Purpose, values_from = Trips)
  28. expect_equal(key_vars(out2), c("Region", "State"))
  29. expect_equal(ncol(out2), 7)
  30. out3 <- tourism %>%
  31. pivot_wider(names_from = State, values_from = Trips)
  32. expect_equal(key_vars(out3), c("Region", "Purpose"))
  33. expect_equal(ncol(out3), 8 + 3)
  34. expect_error(tsbl %>%
  35. pivot_wider(qtr, names_from = qtr, values_from = value), "can't be widened.")
  36. out4 <- tourism %>%
  37. group_by(Purpose) %>%
  38. pivot_wider(names_from = State, values_from = Trips)
  39. expect_is(out4, "grouped_ts")
  40. expect_equal(group_vars(out4), "Purpose")
  41. out5 <- tourism %>%
  42. index_by(year = year(Quarter)) %>%
  43. pivot_wider(names_from = State, values_from = Trips)
  44. expect_is(out5, "grouped_ts")
  45. expect_equal(group_vars(out5), "year")
  46. })
  47. tsbl2 <- tsbl %>%
  48. spread(key = group, value = value)
  49. test_that("pivot_longer()", {
  50. out <- tsbl2 %>%
  51. pivot_longer(x:z, names_to = "key", values_to = "value")
  52. expect_equal(dim(out), c(30, 3))
  53. expect_equal(key_vars(out), "key")
  54. out2 <- tsbl2 %>%
  55. pivot_longer(-qtr, names_to = "key", values_to = "value")
  56. expect_identical(out, out2)
  57. })
  58. test_that("nest()", {
  59. expect_is(pedestrian %>% nest(data = -Date_Time), "tbl_ts")
  60. expect_named(tourism %>% nest(Trips = -Purpose), c("Purpose", "Trips"))
  61. expect_named(pedestrian %>% nest(data = -Date_Time), c("Date_Time", "data"))
  62. expect_is(pedestrian %>% nest(data = c(Date, Count)), "tbl_ts")
  63. expect_named(pedestrian %>% nest(data = dplyr::everything()), "data")
  64. expect_named(pedestrian %>% nest(data = -Sensor), c("Sensor", "data"))
  65. expect_named(
  66. pedestrian %>% group_by(Sensor) %>% nest(),
  67. names(pedestrian %>% nest(data = -Sensor))
  68. )
  69. expect_named(pedestrian %>% nest(ts = -Sensor), c("Sensor", "ts"))
  70. nested_ped <- pedestrian %>%
  71. nest(data = -Sensor)
  72. expect_equal(key_vars(nested_ped$data[[1]]), character(0))
  73. })
  74. nest2_t <- tourism %>%
  75. group_by_key() %>%
  76. summarise(
  77. value = list(quantile(Trips, c(0.3, 0.5, 0.7))),
  78. qtl = list(c(3, 5, 7))
  79. )
  80. test_that("unnest_tsibble()", {
  81. expect_error(nest2_t %>% unnest_tsibble(cols = c(value, qtl)), "A valid tsibble.")
  82. out <- nest2_t %>%
  83. unnest_tsibble(cols = c(value, qtl), key = c(key_vars(tourism), qtl))
  84. expect_is(out, "tbl_ts")
  85. expect_equal(NCOL(out), 6)
  86. })
  87. harvest <- tsibble(
  88. year = c(2011, 2013, 2014, 2010, 2012, 2014),
  89. fruit = rep(c("kiwi", "cherry"), each = 3),
  90. kilo = sample(1:10, size = 6),
  91. key = fruit, index = year
  92. )
  93. harvest_fill <- fill_gaps(harvest, .full = TRUE)
  94. test_that("fill()", {
  95. expect_equivalent(
  96. harvest_fill %>%
  97. group_by_key() %>%
  98. fill(kilo, .direction = "down"),
  99. harvest_fill %>%
  100. as_tibble() %>%
  101. group_by(fruit) %>%
  102. fill(kilo, .direction = "down")
  103. )
  104. expect_equivalent(
  105. harvest_fill %>%
  106. fill(kilo, .direction = "down"),
  107. harvest_fill %>%
  108. as_tibble() %>%
  109. fill(kilo, .direction = "down")
  110. )
  111. expect_is(fill(harvest_fill, kilo), "tbl_ts")
  112. expect_is(fill(group_by_key(harvest_fill), kilo), "grouped_ts")
  113. })
  114. test_that("drop_na() #173", {
  115. expect_equivalent(
  116. harvest_fill %>%
  117. group_by_key() %>%
  118. drop_na(),
  119. harvest_fill %>%
  120. as_tibble() %>%
  121. group_by(fruit) %>%
  122. drop_na()
  123. )
  124. expect_equivalent(
  125. harvest_fill %>%
  126. drop_na(),
  127. harvest_fill %>%
  128. as_tibble() %>%
  129. drop_na()
  130. )
  131. expect_is(drop_na(harvest_fill), "tbl_ts")
  132. expect_is(drop_na(group_by_key(harvest_fill)), "grouped_ts")
  133. })