##  Ch04 \[XR[h

# CSVt@Cǂ݂
dau <- read.csv("section4-dau.csv", header = T, stringsAsFactors = F)
head(dau)
user.info <- read.csv("section4-user_info.csv", header = T, stringsAsFactors = F)
head(user.info)

# DAŨf[^user.infof[^
dau.user.info <- merge(dau, user.info, by = c("user_id", "app_name"))
head(dau.user.info)

# ZOǵiʂŏWvj
dau.user.info$log_month <- substr(dau.user.info$log_date, 1, 7)
table(dau.user.info[, c("log_month", "gender")])

# ZOǵiNŏWvj
table(dau.user.info[, c("log_month", "generation")])

# ZOǵiʁ~NŏWvj
library(reshape2)
dcast(dau.user.info, log_month ~ gender + generation, value.var = "user_id",
length)

# ZOǵifoCXƂŏWvj
table(dau.user.info[,c("log_month","device_type")])

# ZOg͂̌ʂ

# tʃfoCXʂɃ[UZo
dau.user.info.device.summary <- ddply(dau.user.info, .(log_date, device_type), summarize, dau = length(user_id))
# t^ɕϊ
dau.user.info.device.summary$log_date <- as.Date(dau.user.info.device.summary$log_date)
# ñghOt̕`
library(ggplot2)
library(scales)
limits <- c(0, max(dau.user.info.device.summary$dau))
ggplot(dau.user.info.device.summary, aes(x=log_date, y=dau, col=device_type, lty=device_type, shape=device_type)) +
geom_line(lwd=1) +
geom_point(size=4) +
scale_y_continuous(label=comma, limits=limits)