library(vroom)
library(here)
library(dplyr)
library(magrittr)
library(fs)
library(tidyverse)
options(dplyr.summarise.inform = FALSE)
devtools::load_all()

1 - Split-Apply-Combine

This s technique for taking a dataset. Splitting it up for something (i.e. male/female). Doing something to it, and bringing it back.

mmash %>% 
  group_by(day) %>% 
  summarise(across(
    c(age, weight, height),
    list(mean = mean, sd = sd, max = max, min = min),
    na.rm = T
  ))

2 - Pivot longer

mmash %>% 
  select(-samples) %>%
  pivot_longer(c(-user_id, -day, -gender))

Use pivot_longer() after the group_by() and summarise() we did previously:

# Data
mmash %>% 
    group_by(day) %>% 
    summarise(across(
        c(age, weight),
        list(mean = mean, sd = sd), 
        na.rm = TRUE
    ))

# Pivot
mmash %>% 
    group_by(day) %>% 
    summarise(across(
        c(age, weight),
        list(mean = mean, sd = sd), 
        na.rm = TRUE
    )) %>%
  pivot_longer(ends_with(c("mean", "sd")), 
               names_to = c("names", ".value"),
               names_sep = "_")

Pibot wider

We can actually utilize pivot_longer function of naming stuff, to enable us to more easially use pivot_wider.

mmash %>% 
    group_by(day) %>% 
    summarise(across(
        c(age, weight),
        list(mean = mean, sd = sd), 
        na.rm = TRUE
    )) %>%
  pivot_longer(ends_with(c("mean", "sd")),
               names_to = c("names", "summary_statistic"),
               names_sep = "_") %>%
  pivot_wider(names_from = summary_statistic,
              values_from = value)

Do it using a funciton

tidy_summarize_by_day(mmash, c("age", "weight"))
tidy_summarize_by_day(mmash, c(age, weight))

Non-standard evaluation is the thing with {{}}

You can make a tables in HTML using the kable function in the knitr package

tidy_summarize_by_day(mmash, c(age, weight)) %>% 
  knitr::kable(caption = "Nice table")


AndersAskeland/LearnR3 documentation built on Sept. 15, 2020, 10:39 a.m.