Skip to content

Instantly share code, notes, and snippets.

View Andrei-WongE's full-sized avatar
💭
I may be slow to respond.

Andrei-WongE Andrei-WongE

💭
I may be slow to respond.
View GitHub Profile
@acoppock
acoppock / power_depends_on_arms
Created June 27, 2022 13:37
DeclareDesign simulation to see how power changes as we increase the number of arms (with corrections)
library(DeclareDesign)
library(tidyverse)
library(geomtextpath)
design_1 <-
declare_model(N = 1000,
U = rnorm(N),
Y_Z_0 = U,
Y_Z_1 = U + 0.2) +
declare_assignment(Zk = complete_ra(N = N, num_arms = k)) +
# adapted from https://glin.github.io/reactable/articles/cookbook/cookbook.html#nested-tables
library(dplyr)
data <- MASS::Cars93[18:47, ] %>%
mutate(ID = as.character(18:47), Date = seq(as.Date("2019-01-01"), by = "day", length.out = 30)) %>%
select(ID, Date, Manufacturer, Model, Type, Price)
sales_by_mfr <- group_by(data, Manufacturer) %>%
summarize(Quantity = n(), Sales = sum(Price)) |>
@charliejhadley
charliejhadley / generate-maps-for-all-states.R
Created April 12, 2022 19:41
generate-maps-for-all-states.R
library(sf)
library(readxl)
library(janitor)
library(rmapshaper)
library(tigris)
library(tidycensus)
library(tidyverse)
library(furrr)
library(here)
@BenjaminWolfe
BenjaminWolfe / curl.R
Last active April 3, 2022 21:31
Create Pages in Notion with R
# See `curl.md`. Here is the R equivalent.
library(httr)
library(jsonlite)
make_page <- function(url, headers, data) {
response <- POST(
url = url,
body = toJSON(data, auto_unbox = TRUE),
config = add_headers(.headers = headers)
@avallecam
avallecam / write_multiple_purrr.R
Last active February 18, 2023 17:49
write (or export to different files) multiple data frames inside a tibble using purrr
# https://stackoverflow.com/a/70956247/6702544
``` r
# use functions from the
# tidyr, stringr and purrr packages
library(tidyverse)
# create fake dataset
expand_grid(cat = c("a","b"),
lev = "c",
num = 1:2) %>%
@sneakers-the-rat
sneakers-the-rat / e_hashs.json
Last active June 10, 2024 08:32
Elsevier PDF "hashes"
[
"FCi27mtaKod38ztmGndn-y8NNz.r.lt6SndqGztz_ztr-ngqQm9aMo9eOnMeJntuNntu",
"D2ei2mgqJz9b-m.mGmPqRyLNNnwmOlt7.ywiGmt-Kndr9otqRywv8o9ePmtiNmd2Sn92Tma",
"6U7vcmPuOn9uLnMaGyM7-nLNNntv9lt6RmtaGmweOyMmJnMmSmgmOo9eOnM6LnMaRmM-Tma",
"lXLf8owyQztiMzwqGnMz7zcNNotb7lwf.m9qGzt6Km.qMngqLndqLo9eOotaNm96Mmt6Tma",
"FCi27y9qOnd-Ny96GmPmOmcNNzwf-lwj-m9mGztz7ytaMnM78n9v-o9ePmM6Rm9-Qn9eTma",
"XlEDumMz7nM7-m9iGogmRmLNNyt_8lwiKz9eGm9-Pm.v7ztiLztz_o9eOnMeQnd-Sodm",
"lXLf8yt-JywmNmPeGm9n9n8NNzgn.lt_8zwqGogz7zgn7zt6SyPr-o9eOnM6Pot2Mn9qTma",
"FCi27zgf8mdqMmMeGnMmMy8NNz9eQlweNy.eGmMiMm96Qmgr9nMb-o9ePmtuRmt6JotmTma",
"FCi27nwmKnMeSodeGm.z.y8NNntz.lt-PywmGy9__ngqQmtiPmtb7o9ePmteJotyJoduTma",
@jthomasmock
jthomasmock / rprofile-custom-prompt.R
Created September 29, 2021 21:38
An example of customizing your R prompt with the RProfile.
.First <- function(){
# adapted from: https://lapsedgeographer.london/2020-11/custom-r-prompt/
my_prompt <- function(...) {
git_branch <- suppressWarnings(system("git rev-parse --abbrev-ref HEAD",
ignore.stderr = TRUE, intern = TRUE))
if (length(git_branch) != 0) {
git_msg <- paste0(" @", git_branch)
@jimjam-slam
jimjam-slam / download-groups.r
Last active February 13, 2022 22:55
Download grouped remote files using friendlier group names #rstatstips
library(tidyverse)
# based on {searchable} and https://stackoverflow.com/a/64931927/3246758
# stats::setNames is fine too!
invert <- function(x) {
set_names(names(x), x)
}
# let's map the coded names to our friendly ones!
fruit <- c(
---
title: "Fancy math"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use a
# shrunken down high resolution PNG
@norwegianblueparrot
norwegianblueparrot / bourtange.R
Last active November 25, 2023 03:40
A script to produce a 3D render of Fort Bourtange from LiDAR data
## ----setup,include=FALSE,message=FALSE,warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------
library("here") # 1.0.1
library("sf") # 1.0-14
library("rgl") # 1.2.1
library("geoviz") # 0.2.2
library("raster") # 3.6-26
library("rayshader") # 0.35.7
library("osmdata") # 0.2.5
library("tidyverse") # 2.0.0