-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_table.R
64 lines (53 loc) · 2.07 KB
/
create_table.R
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
59
60
61
62
63
64
rm(list=ls())
# libraries
library("tidyverse")
library("kableExtra")
require("webshot2")
library("flextable")
library("magrittr")
# specify paths and files
path.project <- '/local_path'
file_demo <- paste0(path.project, "/ITQ_demographics.csv")
# set wroking dir
setwd(path.project)
# load data
data_table <- read.csv(file_demo)
## Build table ----
library("table1")
# first look
table1(~ Sex + Age + ISI_score + PSQI_score_PP + sleep_problem_duration + IDSSR_score +
HADS_depression + HADS_anxiety + BAI_score | factor(insomnia_subtype),
data=data_table,
overall=F)
# ------------------------------
# specify some field and functions to improve table
# total number of people with insomnia
n_insomnia <- data_table %>%
filter(insomnia_subtype != "Control group") %>%
nrow()
# custom labels
labels <- list(
variables=list(Sex="Sex",
Age="Age (years)",
ISI_score="Insomnia severity (ISI)",
PSQI_score_PP="Sleep quality (PSQI)",
sleep_problem_duration="Insomnia duration (years) ",
IDSSR_score="Depression severity (IDS-SR)",
HADS_depression="Depression severity (HADS)",
HADS_anxiety="Anxiety (HADS)"),
groups=list( "", c(paste0("Insomnia disorder (N=", n_insomnia, ")"))))
# render functions to specify how to display continuous and categorical variables
my.render.cont <- function(x) {
with(stats.apply.rounding(stats.default(x), digits=2), c("",
"Mean (SD)"=sprintf("%s (± %s)", MEAN, SD)))
}
my.render.cat <- function(x) {
c("", sapply(stats.default(x), function(y) with(y,
sprintf("%d (%0.0f %%)", FREQ, PCT))))
}
# split data by group(insomnia subtype)
strata <- split(data_table, data_table$insomnia_subtype)
# create table using specified fields and render functions
table1(strata, labels, groupspan=c(1,5),
render.continuous=my.render.cont, render.categorical=my.render.cat)
# export or copy + special paste as html