Skip to content

Instantly share code, notes, and snippets.

View alexeyknorre's full-sized avatar

Alex Knorre alexeyknorre

View GitHub Profile
@alexeyknorre
alexeyknorre / server.R
Created January 30, 2018 07:14
Shiny demo: Russian crime statistics
library(shiny)
library(ggvis)
df <- read.csv("https://github.com/alexeyknorre/workshop_undone_2018/raw/master/murder_crimes_by_regions.csv",
encoding = "CP1251",
stringsAsFactors = F)
shinyServer(function(input, output) {
region_popup <- function(x) {
@alexeyknorre
alexeyknorre / app.R
Created March 16, 2018 11:42
A small shiny example with murders in Russian regions
library(shiny)
library(ggvis)
# Load data
df <- read.csv("data/murder_crimes_by_regions.csv", stringsAsFactors = F)
# Define UI
ui <- fluidPage(
titlePanel("Убийства в России"),
div(
@alexeyknorre
alexeyknorre / deaths-in-russia-2017.R
Created April 10, 2018 14:28
Snippet deaths-in-russia-2017 with piping
install.packages(c("readxl","rio","dplyr","ggplot2"))
library(dplyr)
library(ggplot2)
rio::import("http://www.gks.ru/free_doc/2017/demo/t3_3.xls") %>%
slice(-c(1:7, 9:11, 13, 14, 17, 21:23, 27, 29, 38)) %>%
select(1:2) %>%
setNames(c("cause","number")) %>%
mutate(cause = gsub("из них от|в том числе от|1)|\\:", "", cause),
@alexeyknorre
alexeyknorre / west-philly-incidents.R
Created August 22, 2019 04:20
A small R script to see whether you should go beyond 50th Street in West Philly.
library(data.table)
library(lubridate)
library(dplyr)
library(ggmap)
library(stargazer)
# Data: https://www.opendataphilly.org/dataset/crime-incidents
philly <- fread("incidents.csv",
select = c("location_block","text_general_code","dispatch_time","dispatch_date", "lat","lng"))
@alexeyknorre
alexeyknorre / west-philly-uc-2018-crime-heatmap.R
Last active October 17, 2019 18:30
West Philly crime heatmap code in R
library(data.table)
library(lubridate)
library(dplyr)
library(ggmap)
# Data: https://www.opendataphilly.org/dataset/crime-incidents
philly <- fread("data/incidents.csv",
select = c("location_block","text_general_code","dispatch_time","dispatch_date", "lat","lng"))
library(rvest)
link <- "https://ege.hse.ru/rating/2018/75767740/all/"
tab <- read_html(link) %>%
# Detect everything that looks like a table
html_table() %>%
# Choose second element in the list (rvest found two tables)
.[[2]]
@alexeyknorre
alexeyknorre / stargazer_fix.r
Created May 31, 2022 04:50
Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
## Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
# Unload stargazer if loaded
detach("package:stargazer",unload=T)
# Delete it
remove.packages("stargazer")
# Download the source
download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz", destfile = "stargazer_5.2.3.tar.gz")
# Unpack
untar("stargazer_5.2.3.tar.gz")
# Read the sourcefile with .inside.bracket fun
@alexeyknorre
alexeyknorre / nyc_shootings_averages.R
Created June 14, 2023 14:12
Monthly averages for shootings in NYC (2013-2022)
library(tidyverse)
library(ggrepel)
ny_sh <- read.csv("data/NYPD_Shooting_Incident_Data__Historic_.csv")
ny_sh_prep <- ny_sh %>%
mutate(date = mdy(OCCUR_DATE),
year = year(date),
month = month(date,
label = T,