Skip to content

Instantly share code, notes, and snippets.

View Keiku's full-sized avatar
🐢
Slowly but surely.

Keiichi Kuroyanagi Keiku

🐢
Slowly but surely.
View GitHub Profile
@Keiku
Keiku / intersection.py
Created February 7, 2017 04:52
Check intersection.
import pandas as pd
df1 = pd.DataFrame({'id': [1, 2, 3]})
df2 = pd.DataFrame({'id': [2, 3, 4]})
set(df1.id).intersection(set(df2.id))
# Out[73]: {2, 3}
@Keiku
Keiku / cut.py
Created February 8, 2017 02:56
Cut a variable with pandas.
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df['species'] = iris.target
mapping = {0 : 'setosa', 1: 'versicolor', 2: 'virginica'}
iris_df = iris_df.replace({'species': mapping})
iris_df['sepal length (bins)'] = pd.cut(iris_df['sepal length (cm)'], bins=[0, 3, 6, 9], include_lowest=False, right=True)
@Keiku
Keiku / generate_c_code.r
Last active February 9, 2017 08:33
Generate c() function code.
library(stringr)
add_backquotes <- function(x) paste0("`", x, "`")
add_doublequotes <- function(x) paste0("\"", x, "\"")
generate_c_code <- function(x){
vec <- paste0(add_doublequotes(x), sep=",\n")
vec_tail <- str_replace(tail(vec, 1), ",\n", "\n")
vec_head <- head(vec, length(vec) - 1)
vec <- c(vec_head, vec_tail)
@Keiku
Keiku / chisq.test_by_group.r
Created February 10, 2017 01:48
Chi-square testing in each group.
library(dplyr)
library(purrr)
library(broom)
df <- data_frame(
group = rep(letters[1:2], each = 50),
cat1 = letters[round(runif(100) * 5) + 1],
cat2 = letters[round(runif(100) * 3) + 1]
)
@Keiku
Keiku / chisq.test_by_group.r
Last active February 10, 2017 01:49
Chi-square testing in each group.
library(dplyr)
library(broom)
library(lazyeval)
df <- data_frame(
group = rep(letters[1:2], each = 50),
cat1 = letters[round(runif(100) * 5) + 1],
cat2 = letters[round(runif(100) * 3) + 1]
)
@Keiku
Keiku / check_id_sets.r
Created February 16, 2017 06:41
Check duplicate id list of some tables.
library(gplots)
library(dplyr)
library(magrittr)
check_id_sets <- function(ids){
ids_venn <- gplots::venn(ids, show.plot=FALSE)
ids_list <- unlist(as.list(ids_venn))
mat_dim <- c((length(ids_list) / (length(ids)+1)), length(ids)+1)
id_sets <- ids_list %>%
matrix(., mat_dim) %>%
@Keiku
Keiku / tqdm.py
Created February 17, 2017 05:49
Print progress bar.
import time
from tqdm import tqdm
pbar = tqdm(["1", "2", "3", "4", "5"])
for char in pbar:
pbar.set_description("Processing %s" % char)
time.sleep(1)
# 0%| | 0/5 [00:00<?, ?it/s]
# Processing 1: 20%|██████▏ | 1/5 [00:01<00:04, 1.00s/it]
# Processing 2: 40%|████████████▍ | 2/5 [00:02<00:03, 1.00s/it]
@Keiku
Keiku / extract_subset.r
Last active February 20, 2017 07:12
Extract a set from the multiple vectors.
a <- c(1, 3, 5, 7, 9)
b <- c(3, 6, 8, 9, 10)
c <- c(2, 3, 4, 5, 7, 9)
intersect_all <- function(...) Reduce(intersect, list(...))
union_all <- function(...) Reduce(union, list(...))
intersect_all(a, b, c)
# [1] 3 9
union_all(a, b, c)
@Keiku
Keiku / tidyr_reshape.r
Last active February 22, 2017 02:21
Reshaping with tidyr
library("dplyr")
library("tidyr")
library("data.table")
smp <- data_frame(
ID = rep(1:3, 2),
BMI = rep(c(21, 26), 3),
sbp = rep(c(150, 120), 3),
nendo = rep(2008:2009, 3)
)
@Keiku
Keiku / dplyr_examples.r
Created February 23, 2017 02:12
The example codes on dplyr package.
library(dplyr)
iris_df <- as_data_frame(iris)
iris_df %>% rename_(.dots = setNames(names(.), toupper(names(.)))) %>% head(2)
# A tibble: 2 × 5
# SEPAL.LENGTH SEPAL.WIDTH PETAL.LENGTH PETAL.WIDTH SPECIES
# <dbl> <dbl> <dbl> <dbl> <fctr>
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa