Skip to content

Commit

Permalink
Single-arg table_one (#48)
Browse files Browse the repository at this point in the history
* add single-arg version of `table_one`

* add tests

* add docs entry

* add changelog entry

* remove a now incorrect test
  • Loading branch information
jkrumbiegel authored Oct 24, 2024
1 parent 4135532 commit 725cad3
Show file tree
Hide file tree
Showing 12 changed files with 2,888 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Added single-argument method to `table_one` which summarizes all columns in the passed table except those used for grouping [#48](https://github.com/PumasAI/SummaryTables.jl/pull/48).
- Fixed issue with using `missing` as a group in `table_one`. This PR also removes redundant group totals if there's just one subgroup to do a total over [#47](https://github.com/PumasAI/SummaryTables.jl/pull/47).

## 3.0.0 - 2024-09-23
Expand Down
15 changes: 13 additions & 2 deletions docs/src/predefined_tables/table_one.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Optionally, there can be grouping applied along the columns as well.

In this example, several variables of a hypothetical population are analyzed split by sex.

```@example
```@example table_one_intro
using SummaryTables
using DataFrames
Expand All @@ -28,6 +28,15 @@ table_one(
)
```

You can also omit the second argument as a shortcut when you quickly want to summarize all columns of your dataset. The columns in `groupby` are excluded automatically:

```@example table_one_intro
table_one(
data,
groupby = :blood_type,
)
```

## Argument 1: `table`

The first argument can be any object that is a table compatible with the `Tables.jl` API.
Expand Down Expand Up @@ -64,12 +73,14 @@ data = [(; x = 1, y = "4"), (; x = 2, y = "5"), (; x = 3, y = "6")]
table_one(data, [:x, :y])
```

## Argument 2: `analyses`
## Optional argument 2: `analyses`

The second argument takes a vector specifying analyses, with one entry for each "row section" of the resulting table.
If only one analysis is passed, the vector can be omitted.
Each analysis can have up to three parts: the variable, the analysis function and the label.

For convenience, if the `analyses` argument is omitted, it is equivalent to passing `Tables.columnnames(table)` except that all columns referenced in `groupby` are filtered out.

The variable is passed as a `Symbol`, corresponding to a column in the input data, and must always be specified.
The other two parts are optional.

Expand Down
13 changes: 13 additions & 0 deletions src/table_one.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,19 @@ function table_one(
Table(cells, header_offset-1, nothing; celltable_kws...)
end

"""
table_one(table; kwargs...)
Create a `table_one` with with all columns from `table` except those used in the `groupby` keyword.
"""
function table_one(table; groupby = [], kwargs...)
groups = make_groups(groupby)
groupsyms = [g.symbol for g in groups]
all_names = Tables.columnnames(table)
all_names_but_groups = setdiff(all_names, groupsyms)
return table_one(table, all_names_but_groups; groupby, kwargs...)
end

tableone_column_header() = CellStyle(halign = :center, bold = true)
tableone_column_header_spanned() = CellStyle(halign = :center, bold = true, border_bottom = true)
tableone_column_header_key() = CellStyle(; halign = :center)
Expand Down
Loading

0 comments on commit 725cad3

Please sign in to comment.