Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In summarise draws, a function that returns a character changes all columns to character #355

Open
n-kall opened this issue Mar 14, 2024 · 4 comments · May be fixed by #356
Open

In summarise draws, a function that returns a character changes all columns to character #355

n-kall opened this issue Mar 14, 2024 · 4 comments · May be fixed by #356

Comments

@n-kall
Copy link
Collaborator

n-kall commented Mar 14, 2024

If a summary function that returns a character is used in summarise_draws, it makes all columns <chr> which messes up formatting.

example_draws() |>
   summarise_draws(x, char_fun = \(x) "word", mean)

Returns

# A tibble: 10 × 3
   variable char_fun      mean
   <chr>    <chr>         <chr>
 1 mu       word          4.1799990610081
 2 tau      word          4.16356885610418
 3 theta[1] word          6.74893947963962
 4 theta[2] word          5.25331634990726
 5 theta[3] word          3.04393475572924
 6 theta[4] word          4.85842854299997
 7 theta[5] word          3.22258991784476
 8 theta[6] word          3.98696993634695
 9 theta[7] word          6.5030995214442
10 theta[8] word          4.56520199862817

This could be fixed by using type.convert(out, as.is = TRUE) on the output tibble in summarise_draws_helper

@mjskay
Copy link
Collaborator

mjskay commented Mar 14, 2024

This suggests to me that the output is being bound into a matrix (which requires a single type) and then converted to a data frame later, which it should probably not be. Rather than potentially round-tripping through a string (with the possibility for information loss) I would suggest binding results together into a data frame directly, which should be more efficient anyway.

@n-kall
Copy link
Collaborator Author

n-kall commented Mar 14, 2024

This suggests to me that the output is being bound into a matrix

Yes, this is indeed the case

@paul-buerkner
Copy link
Collaborator

paul-buerkner commented Mar 14, 2024 via email

@n-kall
Copy link
Collaborator Author

n-kall commented Mar 15, 2024

It seems this is also caused by the behaviour of unlist, which results in a vector, and when performed on a list with different types inside, will convert them all to the same (in this case character). This unlisting is done to handle summary functions that return multiple elements (e.g. quantile2).

However, I don't know of a base R equivalent to purrr::flatten which would do what we want in this case (returning a list, but without nesting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants