tests/testthat/test-dmap.R

test_that("dmap() returns a data frame", {
  expect_s3_class(dmap(mtcars, mean), "data.frame")
})

test_that("dmap() works with sliced data frames", {
  df <- slice_rows(mtcars, "cyl")
  actual <- dmap(df, mean)
  expected <- by_slice(df, dmap, mean, .collate = "rows")
  expect_equal(actual, expected)
})

test_that("dmap() works with no columns to map", {
  res <- mtcars["cyl"] %>% slice_rows("cyl") %>% dmap(mean)
  expect_equal(res, dplyr::group_by(mtcars["cyl"], cyl))
})

test_that("dmap() recycles only vectors of length 1", {
  expect_equal(dmap_at(mtcars, "cyl", mean)$cyl, rep(mean(mtcars$cyl), nrow(mtcars)))
  expect_error(dmap_at(mtcars, c("cyl", "am"), ~1:2), "only recycles")
})

test_that("conditional sliced mapping recycles within groups", {
  df <- mtcars %>% slice_rows(c("vs", "am"))
  expected_df <- by_slice(df, dmap, mean, .collate = "rows")

  res_at <- dmap_at(df, c("disp", "drat"), mean)
  res_if <- dmap_if(df, ~ .x[[1]] == 160, mean)

  expected <- purrr::map2(expected_df$disp, group_sizes(df), rep) %>% purrr::flatten_dbl()
  expect_equal(res_at$disp, expected)
  expect_equal(res_if$disp, expected)
})

Try the purrrlyr package in your browser

Any scripts or data that you put into this service are public.

purrrlyr documentation built on March 29, 2022, 5:05 p.m.