inst/tinytest/test_with_groups.R

df <- data.frame(x = 1:2)
gf <- group_by(df, x)
expect_equal(class(with_groups(df, x, mutate)), "data.frame", info = "restores original class #1")
expect_equal(class(with_groups(gf, x, mutate)), c("grouped_df", "data.frame"), info = "restores original class #2")

gf <- group_by(data.frame(x = 1:2), x)
out <- gf %>% with_groups(NULL, mutate, y = mean(x))
expect_equal(out$y, c(1.5, 1.5), info = ".groups = NULL ungroups")

expect_identical(
  with_groups(mtcars, identity(2), mutate, disp = disp / sd(disp)),
  with_groups(mtcars, 2, mutate, disp = disp / sd(disp)),
  info = ".groups is defused with context"
)

Try the poorman package in your browser

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

poorman documentation built on Nov. 2, 2023, 5:27 p.m.