markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
450 stars 32 forks source link

Column names order in "pivot_wider" with "glue_names" #580

Closed Darxor closed 2 years ago

Darxor commented 2 years ago

Fixes #579

Variable glue_vars now has names, that mimic data.table::dcast's output column names These names are used to map values onto new column names after dcast call

Tests should also probably be added to cover this edge case.

markfairbanks commented 2 years ago

This looks great @Darxor! Thanks for the help 😄

I have a few small changes requested above.

Can you also do me a favor and add this test to the bottom of test-pivot_wider.R?

test_that("correctly labels columns when `names_glue` is used, #579", {
  # length(values_from) == 1
  df1 <- tidytable(
    lettr = c("b", "a", "c"),
    v1 = c("b", "a", "c")
  )

  result1 <- pivot_wider.(
    df1,
    names_from = lettr,
    values_from = v1,
    names_glue = "{.value}_{lettr}"
  )

  expect_named(result1, c("v1_a", "v1_b", "v1_c"))
  expect_equal(unname(unlist(result1)), c("a", "b", "c"))

  # length(values_from) > 1
  df2 <- tidytable(
    lettr = c("b", "a", "c"),
    v1 = c("b", "a", "c"),
    v2 = c("b", "a", "c")
  )

  result2 <- pivot_wider.(
    df2,
    names_from = lettr,
    values_from = c(v1, v2),
    names_glue = "{.value}_{lettr}"
  )

  expect_named(result2, c("v1_a", "v1_b", "v1_c", "v2_a", "v2_b", "v2_c"))
  expect_equal(unname(unlist(result2)), c("a", "b", "c", "a", "b", "c"))
})
Darxor commented 2 years ago

Thanks for your feedback @markfairbanks! All done now, happy to help :)

markfairbanks commented 2 years ago

Thanks again! Would you like to be added to the contributor list? Basically your name would show up here under the authors as a contributor.

Darxor commented 2 years ago

Absolutely! I would be quite honored to see my name there! :)

markfairbanks commented 2 years ago

Is "Alexander Sevostianov" correct for first and last name?

Darxor commented 2 years ago

Yup, it is