inst/doc/x03_Data_Wrangling.R

## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "##",
  fig.width = 6,
  fig.height = 4,
  dpi = 72,
  fig.retina = 1,
  out.width = "90%"
)

library(tidyverse)
library(viridisLite)

theme_set(theme_minimal() + theme(legend.position = "bottom"))

options(
  ggplot2.continuous.colour = "viridis",
  ggplot2.continuous.fill = "viridis"
)
scale_colour_discrete <- scale_colour_viridis_d
scale_fill_discrete <- scale_fill_viridis_d

library("tidyfun")

pal_5 <- viridis(7)[-(1:2)]
set.seed(1221)

## ----view_chf-----------------------------------------------------------------
data(chf_df)

chf_df

## ----plot_chf-----------------------------------------------------------------
chf_df |>
  slice(1:5) |>
  tf_ggplot(aes(tf = activity)) +
  geom_line(alpha = 0.1)

## ----view_dti-----------------------------------------------------------------
data(dti_df)

dti_df

## ----plot_dti-----------------------------------------------------------------
dti_df |>
  tf_ggplot(aes(tf = cca)) +
  geom_line(alpha = 0.05)

## -----------------------------------------------------------------------------
chf_df |>
  select(id, day, activity) |>
  filter(day == "Mon") |>
  tf_ggplot(aes(tf = activity)) +
  geom_line(alpha = 0.05)

## -----------------------------------------------------------------------------
chf_df |>
  group_by(day) |>
  summarize(mean_act = mean(activity)) |>
  tf_ggplot(aes(tf = mean_act, color = day)) +
  geom_line()

## -----------------------------------------------------------------------------
chf_df |>
  slice(1:5) |>
  mutate(exp_act = exp(activity)) |>
  tf_ggplot(aes(tf = exp_act)) +
  geom_line(alpha = 0.2)

## -----------------------------------------------------------------------------
chf_df |>
  select(id, day, activity) |>
  pivot_wider(
    names_from = day,
    values_from = activity
  )

## -----------------------------------------------------------------------------
monday_df <- chf_df |>
  filter(day == "Mon") |>
  select(id, monday_act = activity)
friday_df <- chf_df |>
  filter(day == "Fri") |>
  select(id, friday_act = activity)

## -----------------------------------------------------------------------------
monday_df |>
  left_join(friday_df, by = "id") |>
  pivot_longer(monday_act:friday_act, names_to = "day", values_to = "activity")

## -----------------------------------------------------------------------------
dti_df |>
  group_by(case, sex) |>
  summarize(mean_rcst = mean(rcst, na.rm = TRUE)) |>
  tf_ggplot(aes(tf = mean_rcst, color = case)) +
  geom_line(linewidth = 2) +
  facet_grid(~sex)

## -----------------------------------------------------------------------------
like_to_move_it_move_it <- chf_df |> filter(tf_anywhere(activity, value > 9))
glimpse(like_to_move_it_move_it)

like_to_move_it_move_it |>
  tf_ggplot(aes(tf = activity, colour = id)) +
  geom_line()

## -----------------------------------------------------------------------------
dti_df |>
  filter(tf_anywhere(cca, value < 0.26)) |>
  tf_ggplot(aes(tf = cca)) +
  geom_line()

## -----------------------------------------------------------------------------
chf_df |>
  filter(id == 1) |>
  mutate(smooth_act = tf_smooth(activity)) |>
  tf_ggplot(aes(tf = smooth_act)) +
  geom_line()

## -----------------------------------------------------------------------------
chf_df |>
  group_by(day) |>
  summarize(mean_act = mean(activity)) |>
  mutate(smooth_mean = tf_smooth(mean_act)) |>
  tf_ggplot(aes(color = day)) +
  geom_line(aes(tf = mean_act), alpha = 0.2) +
  geom_line(aes(tf = smooth_mean), linewidth = 2)

## -----------------------------------------------------------------------------
chf_df |>
  filter(id == 1) |>
  mutate(daytime_act = tf_zoom(activity, 360, 1200)) |>
  tf_ggplot(aes(tf = daytime_act)) +
  geom_line(alpha = 0.2)

## -----------------------------------------------------------------------------
dti_df <- dti_df |> mutate(cca_tfb = tfb(cca, k = 25))

## -----------------------------------------------------------------------------
dti_df |>
  slice(1:10) |>
  mutate(
    cca_raw_deriv = tf_derive(cca),
    cca_tfb_deriv = tf_derive(cca_tfb)
  ) |>
  tf_ggplot() +
  geom_line(aes(tf = cca_raw_deriv), alpha = 0.3, linewidth = 0.3, color = "blue") +
  geom_line(aes(tf = cca_tfb_deriv), alpha = 0.3, linewidth = 0.3, color = "red") +
  ylab("d/dt f(t)")

## ----eval = FALSE-------------------------------------------------------------
# withr::with_options(
#   list(data.table.optimize = 0),
#   data.table::as.data.table(chf_df)[, list(mean_act = mean(activity)), by = day]
# )

Try the tidyfun package in your browser

Any scripts or data that you put into this service are public.

tidyfun documentation built on April 24, 2026, 5:06 p.m.