Skip to content

Instantly share code, notes, and snippets.

Avatar

Emi Tanaka emitanaka

View GitHub Profile
View churn_exploration.R
library(data.table)
library(tidyverse) # ggplot2, tidyr, dplyr, purrr, stringr ...
library(scales)
library(grid)
library(RColorBrewer)
###############################################################
## MOST COMMON ACTIONS DURING FIRST TWO WEEKS
###############################################################
## IMPORT AND TIDY DATA
@emitanaka
emitanaka / patch-preserving-attributes-with-c.R
Last active Apr 18, 2021
Combine elements like `c` but preserving the attributes
View patch-preserving-attributes-with-c.R
x <- structure(list(x = 1), "attr" = 2)
y <- structure(list(y = 3), "attr" = 1)
l <- list(x, y)
out <- do.call(c, l)
# doesn't quite work (notice the name)
out2 <- lapply(seq_along(out), function(i) {
attributes(out[[i]]) <- attributes(l[[i]])
out[[i]]
})
View tidymatrix.R
as_tidymatrix <- function(x){
structure(list(.value = as.vector(x),
.row = as.vector(row(x)),
.col = as.vector(col(x))),
row.names = seq_len(length(as.vector(x))),
nrow = nrow(x), # need these attribute if storing it as sparse later
ncol = ncol(x),
class = c("tidymatrix", "data.frame"))
}
@emitanaka
emitanaka / median-income-calc.R
Last active Jun 11, 2020
Median income calculation from range
View median-income-calc.R
library(tidyverse)
income_df <- tibble(income_range = c("0-74", "75-145", "150-325", "325+"),
count = c(125, 170, 215, 250)) %>%
separate(income_range,
into = c("income_min", "income_max"),
convert = TRUE)
calc_median_income <- function(df) {
df %>%
@emitanaka
emitanaka / animate-voting.R
Last active Apr 30, 2020
Instant-runoff voting system for the 🇦🇺 House of Representative election in 2019
View animate-voting.R
# the party logos are required to be in the form of `plogo-XXX.png` where XXX is the party abbreviation that matches
# in PartyAb
# inspired by https://coolbutuseless.github.io/2020/04/12/racing-barplots-with-ggpattern-flagon-gganimate/
library(tidyverse)
library(gganimate)
library(ggpattern)
dat <- read_csv("https://results.aec.gov.au/24310/Website/Downloads/HouseDopByDivisionDownload-24310.csv", skip = 1) %>%
mutate(PartyAb = case_when(
@emitanaka
emitanaka / example.html
Created Sep 11, 2019
remark.js bug - unclosed square bracket
View example.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<style>
.column1, .column2 {
position: absolute;
top:0;
width:50%;
}
@emitanaka
emitanaka / buggy_slide_with_fix.Rmd
Created Sep 11, 2019
xaringan ninjutsu remark.js bug
View buggy_slide_with_fix.Rmd
---
output:
xaringan::moon_reader:
css: "ninjutsu"
seal: false
---
class: split-40
count: false
@emitanaka
emitanaka / collapseoutput.js
Created Jul 20, 2019
Collapsible Code Output for `xaringan`
View collapseoutput.js
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Output</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {
View keybase.md

Keybase proof

I hereby claim:

  • I am emitanaka on github.
  • I am emitanaka (https://keybase.io/emitanaka) on keybase.
  • I have a public key ASBn0EYjrw-NsypB8fDZvX1xNNWZPPkVzwt2bupVc7zt6Ao

To claim this, I am signing this object:

@emitanaka
emitanaka / hadleyinsydney.R
Last active May 1, 2019
Hadley in Sydney Public Lecture
View hadleyinsydney.R
# the power of piping + functional programming + stringr + some gluing <3
library(tidyverse)
tidyverse <- "Hadley"
"You're" -> Hadley
"super" -> Thanks
"{tidyverse} is awesome" %>%
str_glue() %>%