tests/testthat/test-unite_dt.R

test_that("unite a data.frame", {

  df <- expand.grid(x = c("a", NA), y = c("b", NA))
  expect_equal(
    df %>% unite_dt("z", x:y, remove = FALSE),
    data.table(
      x = as.factor(c("a", NA, "a", NA)),
      y = as.factor(c("b", "b", NA, NA)),
      z = c("a_b", NA, NA, NA)
    ),check.attributes = FALSE
  )

  expect_equal(
    df %>% unite_dt("z", x:y, na2char = TRUE, remove = FALSE),
    data.table(
      x = as.factor(c("a", NA, "a", NA)),
      y = as.factor(c("b", "b", NA, NA)),
      z = c("a_b", "NA_b", "a_NA", "NA_NA")
    ),check.attributes = FALSE
  )

  expect_equal(
    df %>% unite_dt("xy", x:y, remove = FALSE),
    data.table(
      x = as.factor(c("a", NA, "a", NA)),
      y = as.factor(c("b", "b", NA, NA)),
      xy = c("a_b", NA, NA, NA)
    ),check.attributes = FALSE
  )
})

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.