50 min approx
#
: commentsname <- object
: assignmentfuns(arg = value)
: function call?fun
: help?"+"
: help on operatorpkg::fun
: function from package?pkg::fun
: help on function from package\(x) x + 1
: anonymous function definition(\(x) x + 1)()
: anonymous function callf(x, y) == (x |> f(y))
: pipelogical
< integer
< numeric
< character
library(tidyverse)
wrapper for many packages:
library(tidyr)
: reshape to tidy (pivot_longer
, pivot_wider
, separate
)library(dplyr)
: data wrangling (filter
, select
, mutate
, group_by
, summarise
)library(forcats)
: factors manipulation (fct_*
)library(lubridate)
: dates manipulation (ymd
, mdy
, dmy
, ymd_hms
)library(stringr)
: string manipulation (str_*
)library(ggplot2)
: data visualizationTidy principles:
here::here()
: project roothere::here("data-raw")
: raw datahere::here("data")
: preprocessed datahere::here("output")
: outputhere::here("R/functions.R")
: script with custom functionshere::here("analyses")
: analysessource(here("R/functions.R"))
read.csv
read_csv
read_sas
, read_spss
, read_stata
rio::import
: import from many formatsrio::export
: export to many formatsjanitor::clean_names
: clean column namesjanitor::remove_empty
: remove empty rows and columnstidyr::fill
): Fills missing values in selected columns using the next or previous entrytidyr::drop_na
): drop rows with missing values in selected columnsunheader::mash_colnames
): combine header rows (including grouping headers with sliding_headers = TRUE
)library(rio)
library(here)
library(unheadr)
library(janitor)
library(tidyverse)
options(rio.import.class = "tibble")
here(
"data-raw",
"Copenhagen_raw.xlsx"
) |>
import(
header = FALSE,
na = c("", "??")
) |>
mash_colnames(
keep_names = FALSE,
n_name_rows = 4,
sliding_headers = TRUE
) |>
clean_names() |>
remove_empty(c("rows", "cols")) |>
fill(demo_sex)
dplyr::filter
: filter rowsdplyr::select
: select columnsdplyr::mutate
: add/change columns (does not affect number of rows)dplyr::summarise
: summarize groups (resulting in one row per group tibble
/data.frame
).by
= <tidy-select>
: group rows by one or more column unique values; used both in mutate
and summarise
.Caution
Summary functions (e.g., min, max):
Vectorized functions (e.g., pmin, pmax):
gtsummary::tbl_summary
: summary tablegtsummary::tbl_cross
: cross tablegtsummary::tbl_uvregression
): regression tablegtsummary::tbl_merge
): merge tables (horizontally)gtsummary::tbl_stack
): stack tables (vertically)gt::gtsave(as_gt(<tbl>), "my_tbl.<ext>")
: save table as <ext>
file (e.g., html
, png
, pdf
, docx
, …)trial |>
tbl_summary(
by = trt,
include = c(trt, age, grade, response),
label = list(
age ~ "Age (years)",
grade ~ "Grade",
response ~ "Response"
),
type = list(
response ~ "categorical"
),
percent = "row",
digits = list(
age ~ 2
),
statistic = list(
all_continuous() ~ "{mean} ({sd})",
response ~ "{n} ({p}%)"
)
) |>
add_n() |>
add_overall() |>
add_p() |>
bold_p(t = 0.6) |>
bold_levels() |>
bold_labels() |>
italicize_levels() |>
italicize_labels()
p <- <DATA> |>
ggplot(
aes(<GLOBAL_MAPPINGS>)
) +
<GEOM_FUNCTION>(
aes(<LOCAL_MAPPINGS>),
position = <LOCAL_POSITION>,
<AESTHETIC> = <LOCAL_CONSTANT>
) +
<SCALE_FUNCTION> +
<FACET_FUNCTION> +
labs(
## aesthetics
<AES_NAME> = "<TEXT>",
## meta-data
<METADATA_NAME> = "<TEXT>"
) +
<THEME>()
p
ggsave("my_plot.png") # last printet plot
ggsave("my_plot.jpeg", p) # specific plot
One month after the end of all the three editions of the course, we will upload a new complete assessment to the website to permit you to check long-term retention of the concepts and skills learned during the course.
The slides are made using Posit’s Quarto open-source scientific and technical publishing system powered in R by Yihui Xie’s Kintr.
This work by Corrado Lanera, Ileana Baldi, and Dario Gregori is licensed under CC BY 4.0
UBEP’s R training for supervisors