Skip to content

Instantly share code, notes, and snippets.

library(tidyverse)
library(logspline)
library(spatstat.core)

set.seed(123)
x <- rnorm(100)

# comparing speeds of building different CDF's
microbenchmark::microbenchmark(
library(tidyverse)

penguins <- palmerpenguins::penguins %>% na.omit()

### ACTUALLY THE BEST WAY TO DO THIS WOULD BE LIKE:
# penguins %>% 
#   group_nest(species, year) %>% 
#   pivot_wider(names_from = year,
#               values_from = data)
library(tidyverse)

defaultW <- getOption("warn")
options(warn = -1)

data(lending_club, package = "modeldata")
df_ca <- filter(lending_club, addr_state == "CA")
df_tx <- filter(lending_club, addr_state == "TX")
@brshallo
brshallo / cdf_density.R
Last active January 21, 2022 08:12
copied from deprecated spatstat:::CDF.density() function
# copied from spatstat.utils::paren()
paren <- function (x, type = "(") {
if (length(x) == 0)
return(x)
if (identical(type, ""))
type <- "blank"
switch(type, `(` = {
out <- paste("(", x, ")", sep = "")
}, `[` = {
out <- paste("[", x, "]", sep = "")
library(tidyverse)
set.seed(1234)
data <- map(1:3, ~runif(10)) %>%
set_names(paste0("x", 1:3)) %>%
as_tibble() %>%
mutate(letters = letters[1:10])
data
library(tidyverse)
library(Hmisc)

data(lending_club, package = "modeldata")
lending_club <- mutate(lending_club, funded_amnt = as.double(funded_amnt))

bootstap_grouped_weighted <- function(df, target, groups, wt){
  options(dplyr.summarise.inform = FALSE)
  
library(tidyverse)

# Setting-up example hiearchy data
rep_unlist <- function(vec, n){
  map(vec, rep, n) %>% unlist()
}

truth <- tibble(
  country = rep_unlist(c("USA", "Canada"), 4),
@brshallo
brshallo / many-models-example.md
Last active October 27, 2021 22:54
Example for R4DS book club
library(tidyverse)

mods_stats <- iris %>% 
  as_tibble() %>% 
  group_by(Species) %>% 
  nest() %>% 
  mutate(lm_mods = map(data, ~lm(Sepal.Length ~ Sepal.Width, data = .x))) %>% 
  mutate(summary_stats = map(lm_mods, broom::glance))
library(ggplot2)
library(dplyr)
library(purrr)

# Binomial distribution method
probs_survive <- 
  tibble(survivors = 1:16) %>% 
  mutate(prob = dbinom(16 - survivors, size = 18, prob = 0.5))
@brshallo
brshallo / row-stuff.md
Created October 20, 2021 23:59
Question answered for R4DS mentor hours
library(tidyverse)

tibble(id = c(3:7, NA, NA, 10:12, NA, NA)) %>% 
  mutate(NA_flag = is.na(id),
         NA_ID = cumsum(!NA_flag & lead(NA_flag))) %>% 
  group_by(NA_ID) %>% 
  mutate(NA_count = cumsum(NA_flag) * NA_flag,
         id_new = ifelse(!NA_flag, NA, min(id, na.rm = TRUE) + NA_count),
         id_final = ifelse(NA_flag, id_new, id))