Zettelkasten

top

R

Subpages

Custom Code

Random R code

#tidyverse
nest_by
± plots in een tabel
mutate_if(is_character, factor)

#skimr
skim

#GGally
data %>% ggpairs(mapping = aes(color=children))


#tables
gt/tidyverse/gtsummary/tableone - https://gt.rstudio.com/articles/intro-creating-gt-tables.html
 + http://www.danieldsjoberg.com/gtsummary/
flextable/officeverse - https://ardata-fr.github.io/officeverse/index.html



https://cmdlinetips.com/2019/04/introduction-to-nest-in-tidyr/
https://cmdlinetips.com/2019/05/how-to-do-pca-in-tidyverse-framework/




library(tidyverse)
library(ggprism)
library(glue)

theme_set(theme_prism())

xxx <- mtcars %>%
  nest_by(cyl) %>% 
  mutate(
    plot = list(ggplot(
          data = data,
          mapping = aes(
          	x = hp,
          	y = mpg
          )
        ) +
        ggtitle(
          glue::glue("Number of Cylinder: {cyl}")
        ) +
        geom_point()
    )
  )

tidy_pca_nest$pca %>%
  map(~tidy(.x, data = .y, "pcs")) %>%
  as.data.frame()


library(gapminder)
library(broom)
#broom::glance()

gapminder_df <- gapminder %>% 
  nest_by(continent) %>%
  mutate(
    fit = lm(
        lifeExp ~ gdpPercap,
        data = data
      ) %>%
      broom::tidy() %>%
      list()
  )




library(janitor)
tabyl(mydata, Gender, LanguageGroup)



rmarkdown::render('foo.Rmd', 'pdf_document')


swirl::swirl()



x %>% 
    select(sort(names(x))) %>%   # sort the names so the f columns and d columns correspond
    mutate(agg = {
        fs = select(., starts_with('f')) 
        ds = select(., starts_with('d'))
        rowSums(fs * ds) / rowSums(fs) 
    })



tdir <- tempfile('CIBERSORT')
dir.create(tdir)
owd <- setwd( tdir )
on.exit({
  unlink(tdir, recursive = TRUE)
  setwd(owd)
})

---
library(gt)
library(tidyverse)
library(glue)

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  dplyr::select(-adj_close) %>%
  gt() %>%
  tab_header(
    title = "S&P 500",
    subtitle = glue::glue("{start_date} to {end_date}")
  ) %>%
  fmt_date(
    columns = vars(date),
    date_style = 3
  ) %>%
  fmt_currency(
    columns = vars(open, high, low, close),
    currency = "USD"
  ) %>%
  fmt_number(
    columns = vars(volume),
    suffixing = TRUE
  )