tests/testthat/test-summarise_dt.R

test_that("summarise", {
  ir = as.data.table(iris)

  expect_equal(
    iris %>% summarise_dt(avg = mean(Sepal.Length)),
    ir[,.(avg = mean(Sepal.Length))]
  )
  expect_equal(
    iris %>% summarise_dt(avg = mean(Sepal.Length),by = Species),
    ir[,.(avg = mean(Sepal.Length)),by = Species]
  )

  expect_equal(
    iris %>% summarise_vars(is.numeric,mean) %>% as.numeric(),
    ir[,1:4] %>% colMeans() %>% as.numeric()
  )
  expect_equal(
    iris %>% summarise_vars(is.numeric,min),
    iris %>% summarise_vars(-is.factor,min)
  )
  expect_equal(
    iris %>% summarise_vars(is.numeric,min),
    iris %>% summarise_vars(1:4,min)
  )
  expect_equal(
    iris %>% summarise_vars(is.numeric,min,by ="Species"),
    ir[,lapply(.SD,min), by = "Species"]
  )
})
hope-data-science/tidyfst documentation built on Sept. 23, 2024, 8:05 p.m.