Rows: 157 Columns: 38
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): condition, gender, race, parent_income
dbl (33): experiment, participant, age_num, pas, pets_now, pets_child, dog_...
dttm (1): date
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
`summarise()` has grouped output by 'condition'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups: condition [2]
condition prepost panas
<fct> <fct> <dbl>
1 Control Pre 2.99
2 Control Post 2.76
3 HAI Pre 2.97
4 HAI Post 3.23
Tables by {knitr}
The {knitr} package uses the kable() function to format tables.
library(knitr)kable(condition_prepost_means)
condition
prepost
panas
Control
Pre
2.989873
Control
Post
2.760759
HAI
Pre
2.971795
HAI
Post
3.232051
Column and row names
You can control column names and row names with col.names and row.names.
By default, character columns are left aligned and numeric columns are right aligned. You can set alignment manually with the align argument with l = left, c = center, and r = right. You can just pass a character string with a series of those letters.
Add a title to the table with the caption argument. The good news is that we can cross-reference easily (Table @ref(tab:title-table)). The bad news is that with captions, tables in PDFs are automatically placed at the top of the page. We’ll see how to fix this later.
kable(condition_prepost_means,col.names =c("Condition", "Prepost", "Mean PANAS"), caption ="PANAS scores by condition and prepost")
PANAS scores by condition and prepost
Condition
Prepost
Mean PANAS
Control
Pre
2.989873
Control
Post
2.760759
HAI
Pre
2.971795
HAI
Post
3.232051
Supplementing kable with {kableExtra}
The kable() function is intentionally simple to use and therefore does not have a lot of additional functionality. The {kableExtra} package supplements the kable() functionality with additional formatting options by adding additional functions after the kable() function call using the |> pipe (a bit like how ggplot() works). Check out Create Awesome LaTeX Table with knitr::kable and kableExtra.
The following object is masked from 'package:dplyr':
group_rows
General styling
The kable_styling() function formats a number of things such as font size, table width, and table alignment. I’ll also add latex_options = "hold_position" to keep the table in the text. Otherwise, it puts it at the top of the page.
kable(condition_prepost_means,caption ="PANAS scores by condition and prepost",col.names =c("Condition", "Prepost", "Mean PANAS"), booktabs =TRUE) |>kable_styling(font_size =18, latex_options ="hold_position")
PANAS scores by condition and prepost
Condition
Prepost
Mean PANAS
Control
Pre
2.989873
Control
Post
2.760759
HAI
Pre
2.971795
HAI
Post
3.232051
Labels spanning rows
If you want to label groups of rows, use pack_rows(). Let’s get rid of the condition column and label the conditions explicitly.
The {papaja} package uses the kable() function to format tables in APA format with the apa_table() function. You can use many of the same arguments that are available in the kable() function. You can control where the table is placed (here, top, bottom) with the placement argument. You can add a general footnote with the note argument.
apa_table(condition_prepost_means,caption ="PANAS scores by condition and prepost",col.names =c("Condition", "Prepost", "Mean PANAS"),placement ="h",note ="Source: Thayer & Stevens (2021)")
(#tab:unnamed-chunk-19) PANAS scores by condition and prepost
Condition
Prepost
Mean PANAS
Control
Pre
2.99
Control
Post
2.76
HAI
Pre
2.97
HAI
Post
3.23
Note. Source: Thayer & Stevens (2021)
Notice the alignment is different, with everything left aligned. Let’s right align the means.