Skip to content

Instantly share code, notes, and snippets.

@gghatano
gghatano / doParallel.R
Created September 13, 2014 11:43
スキあらば並列計算をするおじさん
library(dplyr)
randomwalk2D <- function(){cbind(x=cumsum(rnorm(100)), y=cumsum(rnorm(100)))}
library(foreach)
library(doParallel)
registerDoParallel(detectCores())
res_df = foreach(i = 1:10, .combine = rbind) %dopar%
data.frame(randomwalk2D(), group=LETTERS[i])
@gghatano
gghatano / progress.R
Created August 22, 2014 06:27
進捗どうですか
> library(cowsay)
> say("進捗どうですか")
-----
進捗どうですか
------
\ ^__^
\ (oo)\ ________
(__)\ )\ /\
||------w|
@gghatano
gghatano / md2html.bash
Created August 15, 2014 12:05
マークダウンをhtmlにするだけです
#!/bin/bash
dir=$(dirname $1)
[ "$dir" = "" ] && exit 1
pandoc -f markdown -t html $1 -o $dir/html
@gghatano
gghatano / Rmd2R
Last active August 29, 2015 14:02
RmdファイルからRコードを抜き出して実行までやってみる
RmdFileName=$1
FileName=`basename $1 .Rmd`
echo -e "library(knitr) \n purl(\"$RmdFileName\") \n source(\"$FileName.R\")"\
| R --no-save
rm $FileName".R"
@gghatano
gghatano / dt_df.R
Created June 23, 2014 23:32
data.tableをinner_joinするとdata.frameになって困る?
library(data.table)
library(dplyr)
dt1 = data.table(x = 1, y = 1)
dt2 = data.table(x = 1, z = 1)
dt1 %>% inner_join(dt2, by = "x")
# x y z
#1 1 1 1
@gghatano
gghatano / matrix_Rcpp.R
Last active August 29, 2015 14:02
Rcppで行列処理
library(Rcpp)
sourceCpp("timesTwo.cpp")
x = matrix(1:9, nrow = 3, ncol= 3)
timesTwo(x)
@gghatano
gghatano / kill_for_loop.R
Last active August 29, 2015 14:02
for文絶対殺すマン
# 関数定義
func = function(hoge){
return(hoge*hoge)
}
# 1から10までの整数のベクトルに対して関数を適用したい
x = 1:10
# 絶対殺す
fx = as.numeric()
@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},
@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
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)