-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr-code-reference.qmd
More file actions
58 lines (48 loc) · 1.18 KB
/
r-code-reference.qmd
File metadata and controls
58 lines (48 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
title: "R Code Reference"
author: "Win Khaing"
format: pdf
editor: visual
---
```{r include=FALSE}
knitr::opts_chunk$set(message=FALSE)
```
```{r}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, tidyr, dplyr, tidyverse, knitr, finalfit, here)
```
## Contigency Table (2x2 Table)
```{r}
library(tidyverse)
library(gtsummary)
data(CO2)
head(CO2)
# basic summary table
CO2 %>% select(!c(Plant, conc)) %>% tbl_summary()
# summary split by a categorical variable
CO2 %>% select(!c(Plant, conc)) %>% tbl_summary(by = Type)
# summary split by a categorical variable with p-value
CO2 %>% select(!c(Plant, conc)) %>%
tbl_summary(by = Type) %>% add_p()
# include overall, extra heading, custom stats
CO2 %>% select(!c(Plant, conc)) %>%
tbl_summary(
by = Type,
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)"
),
digits = all_continuous() ~ 2
) %>%
add_p() %>% add_overall() %>%
modify_spanning_header(c("stat_1", "stat_2") ~ "**Location**") %>%
bold_labels()
# crosstabs
CO2 %>%
tbl_cross(
row = Type,
col = Treatment, percent = "cell"
) %>%
add_p() %>%
bold_labels()
```