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

Feature request: Listing table for multiple columns #59

Open
PharmCat opened this issue Dec 5, 2024 · 1 comment
Open

Feature request: Listing table for multiple columns #59

PharmCat opened this issue Dec 5, 2024 · 1 comment

Comments

@PharmCat
Copy link

PharmCat commented Dec 5, 2024

listingtable is great but it make table with only one variable. will be very useful to make similar table with row grouping and statistics with multiple columns - without 'cols' grouping variable.

Yes, I can make DataFrame stack-unstack but in this case I can't make pretty description for column header (as I can do in table_one).

@jkrumbiegel
Copy link
Collaborator

You could change the descriptions by replacing them with a utility function. I'm not sure if I would like to extend the API of this function to handle the multiple-columns case. It seems common enough but I'm not sure what a clean way would be to have this coexist with the current functionality.

using SummaryTables, DataFrames


df = DataFrame(
    group = repeat(["A", "B", "C", "D"], inner = 5),
    id = repeat(1:5, 4),
    value_a = randn(4 * 5),
    value_b = randn(4 * 5),
    value_c = randn(4 * 5),
)

function stack_with_rename(df, cols...)
    _replacer(col::String) = col => col
    _replacer(pair::Pair{String,<:Any}) = pair
    replacers = map(_replacer, cols)
    stacked = stack(df, collect(first.(replacers)))
    stacked.variable .= replace(stacked.variable, replacers...)
    return stacked
end

listingtable(
    stack_with_rename(df, "value_a", "value_b" => "Value B", "value_c" => Multiline("Value", "C")),
    :value,
    rows = [:group, :id],
    cols = :variable,
    sort = false,
    variable_header = false,
)

The sort = false here is necessary because the Multiline value breaks sorting. If you only used strings, that wouldn't be necessary.

grafik

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

No branches or pull requests

2 participants