Skip to content

Instantly share code, notes, and snippets.

@EmilHvitfeldt
EmilHvitfeldt / candy.R
Created October 26, 2017 00:59
Similarly rated candies in the candy hierarchy data for 2017
library(tidyverse)
library(stringr)
require(visNetwork)
candy <- read_csv("candyhierarchy2017.csv")
names(candy) <- names(candy) %>% str_replace_all("\\\\xd5", "")
balanced_candy <- candy %>%
mutate(total_na = candy %>%
select(starts_with("Q6")) %>%
@EmilHvitfeldt
EmilHvitfeldt / quantile_coefficients.R
Created October 26, 2017 22:51
Interactive visualization to show the effect of covariate estimates on the response distribution in quantile regression
library(shiny)
library(shinythemes)
library(shinysense)
library(tidyverse)
ui <- fluidPage(
theme = shinytheme("flatly"),
fluidRow(
column(width = 5,
h4("Drawn coefficients"), shinydrawrUI("outbreak_stats")),
@EmilHvitfeldt
EmilHvitfeldt / one-hot-encoding
Created December 9, 2017 18:21
One hot transformation of catagorical variable in tidyverse
library(tidyverse)
one_hot <- function(data, var) {
var_enquo <- enquo(var)
items <- data %>% pull(!!var_enquo)
items_unique <- items %>% unique()
out <- matrix(0, NROW(data), length(items_unique))
colnames(out) <- items_unique
@EmilHvitfeldt
EmilHvitfeldt / one-hot-encoding
Created December 9, 2017 18:21
One hot transformation of catagorical variable in tidyverse
library(tidyverse)
one_hot <- function(data, var) {
var_enquo <- enquo(var)
items <- data %>% pull(!!var_enquo)
items_unique <- items %>% unique()
out <- matrix(0, NROW(data), length(items_unique))
colnames(out) <- items_unique
library(rvest)
library(tidyverse)
library(lubridate)
library(glue)
#library(ehlib) # devtools::install_github("EmilHvitfeldt/ehlib")
str_between <- function(string, start, end) {
stringr::str_extract(string,
stringr::str_c(start, '(.*?)', end, collapse = '')) %>%
stringr::str_replace(start, "") %>%
@EmilHvitfeldt
EmilHvitfeldt / visualizing-trigrams-with-the-tidyverse.r
Created January 23, 2018 21:59
Visualizing trigrams with the Tidyverse
library(tidyverse)
library(tidytext)
library(purrrlyr)
str_nth_word <- function(x, n, sep = " ") {
str_split(x, pattern = " ") %>%
map_chr(~ .x[n])
}
sigmoid <- function(x_from, x_to, y_from, y_to, scale = 5, n = 100) {
@EmilHvitfeldt
EmilHvitfeldt / points-in-states.r
Created January 31, 2018 17:37
data points in states
library(maps)
library(tidyverse)
# Having the data.csv file in the same working directory
data <- read_csv("data.csv")
data_states <- data %>%
mutate(state = map.where('state', Longitude, Latitude)) %>%
# Removes point not in a state
@EmilHvitfeldt
EmilHvitfeldt / rvision_benchmark.R
Created February 16, 2018 18:19
Frame rate for color blob detection using Rvision
# Packages
library(Rvision)
library(dplyr)
library(lubridate)
# Functions
blob_fun <- function(img, fun, color = character()) {
img %>%
split() %>%
do.call(fun, .) %>%
@EmilHvitfeldt
EmilHvitfeldt / horizontal.R
Created February 25, 2018 06:35
Horizontal annotations with ggrepel and ggplot2
library(tidyverse)
library(ggrepel)
set.seed(1234)
data <- tibble(x = seq_len(100),
y = cumsum(rnorm(100)))
anno_data <- data %>%
filter(x %% 25 == 10) %>%
@EmilHvitfeldt
EmilHvitfeldt / geom_heart.r
Last active February 16, 2021 23:23
geom that uses hearts instead of points.
geom_heart <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
{
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
stop("Specify either `position` or `nudge_x`/`nudge_y`",
call. = FALSE)
}
position <- position_nudge(nudge_x, nudge_y)