Skip to content

Instantly share code, notes, and snippets.

@gghatano
gghatano / pawapuro.R
Created February 11, 2014 15:41
pawapuro batting test score
library(data.table)
library(dplyr)
library(stringr)
# read data
dat = fread("all2013.csv")
fields = fread("fields.csv")
setnames(dat, fields$Header)
# processing datatable
@gghatano
gghatano / monte.r
Created March 2, 2014 15:29
montecalro with Rcpp and inline
library(Rcpp)
library(inline)
cpp_code = "
using namespace Rcpp;
double piSugar(int N){
RNGScope scope;
NumericVector x = runif(N);
NumericVector y = runif(N);
NumericVector d = sqrt(x*x + y*y);
@gghatano
gghatano / spin_rate_vs_hit_or_strike.r
Created March 6, 2014 18:51
calculate the spin_rate distribution under condition : HIT or STRIKE
library(dplyr)
library(magrittr)
library(data.table)
library(ggplot2)
# pitch f/x data 2013
dat = fread("2013.csv")
dat = dat %>% filter(sv_id !="NA")
# hit event
@gghatano
gghatano / linetype.R
Created March 12, 2014 02:15
linetype test (ggplot2)
d=data.frame(lt=c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash", "1F", "F1", "4C88C488", "12345678"))
ggplot() +
scale_x_continuous(name="", limits=c(0,1), breaks=NA) +
scale_y_discrete(name="linetype") +
scale_linetype_identity() +
geom_segment(data=d, mapping=aes(x=0, xend=1, y=lt, yend=lt, linetype=lt))
@gghatano
gghatano / legends.csv
Last active August 29, 2015 13:57
Visualize the records of legend batters (MLB)
playerID careerHit careerHit.x careerHit.y fullname
aaronha01 3771 3771 3771 Hank Aaron
ansonca01 3418 3418 3418 Cap Anson
biggicr01 3060 3060 3060 Craig Biggio
boggswa01 3010 3010 3010 Wade Boggs
brettge01 3154 3154 3154 George Brett
brocklo01 3023 3023 3023 Lou Brock
carewro01 3053 3053 3053 Rod Carew
clemero01 3000 3000 3000 Roberto Clemente
cobbty01 4189 4189 4189 Ty Cobb
library(Lahman)
library(dplyr)
library(magrittr)
library(pings)
# Batting: season stats data frame of all the players
all_dat <- Batting %>%
select(yearID, AB, H, HR) %>%
group_by(yearID) %>%
dplyr::summarise(H = sum(H, na.rm = TRUE),
@gghatano
gghatano / df_merge.R
Created April 16, 2014 23:48
文字は一致, 順番バラバラのコラム持ちデータフレームを結合
df1 = data.frame(a = 1:3, b = 4:6, c = 7:9)
df2 = data.frame(c = 10:12, a = 13:15, b = 16:18)
merge(df1,df2, all=TRUE)
# a b c
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# 4 13 16 10
# 5 14 17 11
library(data.table)
library(dplyr)
library(magrittr)
makedata = function(year = 2013){
# set the path of data file
filename = paste("../../../data/all", year, ".csv", sep="")
dat = fread(filename, header=FALSE)
colnames = fread("names.csv", header = FALSE) %>% unlist
setnames(dat, colnames)
@gghatano
gghatano / combine.sh
Created May 20, 2014 02:58
情報課題レポートをIDでリネーム, ソートして結合.
for dir in `ls | grep [0-9]`
do
mv $dir"/"*.pdf $dir".pdf"
done
join.py --output out.pdf *.pdf
@gghatano
gghatano / hoge.tex
Created May 30, 2014 10:10
texのnatbibで, (hoge et al, 2030)みたいな引用
\documentclass{article}
\usepackage[round]{natbib}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article {Schimd1993,
author = {Schmid, Hans-Peter and McNeal, John E. and Stamey, Thomas A.},
title = {Observations on the doubling time of prostate cancer. The use of serial prostate-specific antigen in patients with untreated disease as a measure of increasing cancer volume},
journal = {Cancer},
volume = {71},