Skip to content

Instantly share code, notes, and snippets.

library(dplyr)
c_sort_collapse <- function(...){
c(...) %>%
sort() %>%
paste(collapse = ".")
}
unique_combs <- function(...){
list(...) %>%
library(tidyverse)
library(tidymodels)
library(workboots)
data <- tibble(x = abs(rnorm(1000)),
y = x + rnorm(1000, sd = 0.7) * x / (max(x))
)
rec <- recipe(y ~ ., data = data)
library(tidyverse)
library(lubridate)

tibble(
  group = c("a", "a", "b", "b"),
  dates = ymd(c("2020-01-01", "20220207", "20220209", "20220115"))
) %>%
  group_by(group) %>% 
 summarise(date_range = list(range(dates) %&gt;% 
library(dplyr)
library(lubridate)

tibble(
  file = c("a", "b", "b", "a"),
  dates = c("2020-01-01", "01-jan-2020", "15-mar-2020", "2020-03-15")
) %>%
  mutate(dates_clean = parse_date_time(dates, orders = c("ymd", "dmy")))
#&gt; # A tibble: 4 x 3
@brshallo
brshallo / get-engagements.R
Last active April 17, 2022 17:32
Function to get all various types of engagements from Twitter for a user
library(rjson)
library(httr)
library(jsonlite)
library(dplyr)
library(purrr)
library(lubridate)
library(rtweet)
library(tidyr)
get_engagements <- function(user_id,
show_in_excel <- function(.data){
tmp <- paste0(tempfile(), ".csv")
write.csv(.data, tmp)
fs::file_show(path = tmp)
}
# devtools::install_github("brshallo/funspotr")
library(funspotr)
library(dplyr)
# I only kept gists with a .R file extension...
# This is a small subset of actual number of R files.
github_gists("cmparlettpelleriti") %>%
filter(funspotr:::str_detect_r_rmd(contents)) %>%
github_spot_funs(custom_urls = .) %>%
unnest_github_results()
@brshallo
brshallo / custom-ggplot-and-labels.R
Last active December 22, 2021 20:52
Nice way to pass in arbitrary labels to plotly::ggplotly()
library(ggplot2)
custom_ggplot <- function(df, x, y, ...){
ggplot(mpg, aes(x = cty, y = hwy, ...))+
geom_point()
}
custom_ggplot(color = class)
p <- custom_ggplot(class = class, displ = displ)
@brshallo
brshallo / stratified-sampling-parameter-estimates.R
Created December 15, 2021 23:51
Answering R4DS learning community question on whether stratified sampling. Generally probably don't need to worry about biasing parameter estimates.
library(tidyverse)
sim_params <- function(n_a = 100, n_b = 400, suffix = ""){
tibble(id = c(rep("a", n_a), rep("b", n_b)),
vals = c(rnorm(n_a, 5), rnorm(n_b, 3))
) %>%
lm(vals ~ id, data = .) %>%
broom::tidy() %>%
select(term, estimate) %>%

Using pwiser

library(tidyverse)
if (!require(pwiser) ) remotes::install_github("brshallo/pwiser")

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

t_test_statistic <- function(x, y){
 t.test(x, y) %&gt;%