This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(broom) | |
mtcars %>% | |
group_by(cyl) %>% | |
do(tidy(t(quantile(.$mpg, probs = seq(0, 1, 0.25))))) | |
# Source: local data frame [3 x 6] | |
# Groups: cyl [3] | |
# | |
# cyl X0. X25. X50. X75. X100. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
freq <- function(df, ...){ | |
df %<>% | |
group_by_(...) %>% | |
summarise(count = n()) %>% | |
arrange_(.dots = ...) %>% | |
ungroup() %>% | |
mutate( | |
cum_count = cumsum(count), | |
percent = count / sum(count), | |
cum_percent = cumsum(percent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(readr) | |
library(data.table) | |
library(feather) | |
object.size(df) | |
# 1654613472 bytes | |
system.time(write_csv(df, "df_write_csv.csv")) | |
# ユーザ システム 経過 | |
# 160.540 29.079 200.667 | |
system.time(fwrite(df, "df_fwrite.csv")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(mlr) | |
set.seed(123, "L'Ecuyer") | |
iris.task = classif.task = makeClassifTask(id = "iris-example", data = iris, target = "Species") | |
resamp = makeResampleDesc("CV", iters = 10L) | |
lrn = makeLearner("classif.rpart") | |
control.grid = makeTuneControlGrid() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(lubridate) | |
df <- data_frame( | |
id = c(1, 1, 1, 2, 2, 2), | |
ym = c("201512", "201601", "201603", "201512", "201602", "201603") | |
) | |
elapsed_months <- function(end, start) { | |
12 * (year(end) - year(start)) + (month(end) - month(start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
data <- data_frame(var = c(0, NA, 2)) | |
data %>% mutate(var = coalesce(var, 1)) | |
data %>% mutate(var = replace(var, which(is.na(var)), 1)) | |
data %>% mutate(var = if_else(is.na(var), 1, var)) | |
# A tibble: 3 × 1 | |
# var | |
# <dbl> | |
# 1 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(mice) | |
library(purrr) | |
map_df(airquality, function(x) sum(is.na(x))) | |
# A tibble: 1 × 6 | |
# Ozone Solar.R Wind Temp Month Day | |
# <int> <int> <int> <int> <int> <int> | |
# 1 37 7 0 0 0 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(tidyr) | |
iris %>% | |
as_data_frame(.) %>% | |
select(matches("Petal")) %>% | |
summarise_all(.funs = c("01:sum" = "sum", | |
"02:min" = "min", | |
"03:q25" = "quantile(., 0.25)", | |
"04:median" = "median", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
# アスタリスクが必要 | |
files = glob.glob('/home/dir1/*.zip') | |
for file in files: | |
print(file) | |
print('/home/dir2/' + os.path.basename(file)) | |
# /home/dir1/subset3.zip | |
# /home/dir2/subset3.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | |
def freq(data, var): |
OlderNewer