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"]
  )
})

Try the tidyfst package in your browser

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

tidyfst documentation built on Sept. 16, 2024, 9:06 a.m.