1. csv 파일 읽어 들이기
> setwd("C:/파일/temp/r_temp")
> dau<-read.csv('section4-dau.csv',header=T,stringsAsFactors = F)
> head(dau)
log_date app_name user_id
1 2013-08-01 game-01 33754
2 2013-08-01 game-01 28598
3 2013-08-01 game-01 30306
4 2013-08-01 game-01 117
5 2013-08-01 game-01 6605
6 2013-08-01 game-01 346
> user.info<-read.csv('section4-user_info.csv',header=T,stringsAsFactors = F)
> head(user.info)
i nstall_date app_name user_id gender generation device_type
1 2013-04-15 game-01 1 M 40 iOS
2 2013-04-15 game-01 2 M 10 Android
3 2013-04-15 game-01 3 F 40 iOS
4 2013-04-15 game-01 4 M 10 Android
5 2013-04-15 game-01 5 M 40 iOS
6 2013-04-15 game-01 6 M 40 iOS
2. DAU에 user.info 결합하기
> dau.user.info<-merge(dau,user.info,by=c('user_id','app_name'))
> head(dau.user.info)
user_id app_name log_date install_date gender generation device_type
1 1 game-01 2013-09-06 2013-04-15 M 40 iOS
2 1 game-01 2013-09-05 2013-04-15 M 40 iOS
3 1 game-01 2013-09-28 2013-04-15 M 40 iOS
4 1 game-01 2013-09-12 2013-04-15 M 40 iOS
5 1 game-01 2013-09-11 2013-04-15 M 40 iOS
6 1 game-01 2013-09-08 2013-04-15 M 40 iOS
3. 월 항목 추가
> dau.user.info$month<-substr(dau.user.info$log_date,1,7)
> head(dau.user.info)
user_id app_name log_date install_date gender generation device_type month
1 1 game-01 2013-09-06 2013-04-15 M 40 iOS 2013-09
2 1 game-01 2013-09-05 2013-04-15 M 40 iOS 2013-09
3 1 game-01 2013-09-28 2013-04-15 M 40 iOS 2013-09
4 1 game-01 2013-09-12 2013-04-15 M 40 iOS 2013-09
5 1 game-01 2013-09-11 2013-04-15 M 40 iOS 2013-09
6 1 game-01 2013-09-08 2013-04-15 M 40 iOS 2013-09
> colnames(dau.user.info)[8]<-'log_month'
> head(dau.user.info)
user_id app_name log_date install_date gender generation device_type log_month
1 1 game-01 2013-09-06 2013-04-15 M 40 iOS 2013-09
2 1 game-01 2013-09-05 2013-04-15 M 40 iOS 2013-09
3 1 game-01 2013-09-28 2013-04-15 M 40 iOS 2013-09
4 1 game-01 2013-09-12 2013-04-15 M 40 iOS 2013-09
5 1 game-01 2013-09-11 2013-04-15 M 40 iOS 2013-09
6 1 game-01 2013-09-08 2013-04-15 M 40 iOS 2013-09
4. 성별로 집계
> table(dau.user.info[,c('log_month','gender')])
gender
log_month F M
2013-08 47343 46842
2013-09 38027 38148
> table(dau.user.info[,c('gender','log_month')])
log_month
gender 2013-08 2013-09
F 47343 38027
M 46842 38148
-> 전체적인 수치는 떨어졌지만 남녀간의 구성비율은 변화가 없으므로, 성별과 게임이용율 간 연관관계가 크지 않음.
5. 연령별로 집계
> table(dau.user.info[,c('log_month','generation')])
generation
log_month 10 20 30 40 50
2013-08 18785 33671 28072 8828 4829
2013-09 15391 27229 22226 7494 3835
> table(dau.user.info[,c('generation','log_month')])
log_month
generation 2013-08 2013-09
10 18785 15391
20 33671 27229
30 28072 22226
40 8828 7494
50 4829 3835
-> 전체적인 수치는 떨어졌지만 연령간의 구성비율은 변화가 없으므로, 연령과 게임이용율 간 연관관계가 크지 않음.
6. 세그먼트 분석(성별과 연령대를 조합해서 집계)
> library(reshape2)
> dcast(dau.user.info,log_month~gender+generation,value.var='user_id',length)
log_month F_10 F_20 F_30 F_40 F_50 M_10 M_20 M_30 M_40 M_50
1 2013-08 9091 17181 14217 4597 2257 9694 16490 13855 4231 2572
2 2013-09 7316 13616 11458 3856 1781 8075 13613 10768 3638 2054
-> 전체 이용율 하락에 영햐을 끼친 세그먼트는 찾아내지 못함
7. 세그먼트 분석(단말기별로 집계)
> head(dau.user.info)
user_id app_name log_date install_date gender generation device_type log_month
1 1 game-01 2013-09-06 2013-04-15 M 40 iOS 2013-09
2 1 game-01 2013-09-05 2013-04-15 M 40 iOS 2013-09
3 1 game-01 2013-09-28 2013-04-15 M 40 iOS 2013-09
4 1 game-01 2013-09-12 2013-04-15 M 40 iOS 2013-09
5 1 game-01 2013-09-11 2013-04-15 M 40 iOS 2013-09
6 1 game-01 2013-09-08 2013-04-15 M 40 iOS 2013-09
> table(dau.user.info[,c('log_month','device_type')])
device_type
log_month Android iOS
2013-08 46974 47211
2013-09 29647 46528
-> ios의 변화율은 없는 반면, android의 변화율이 큼을 알 수 있다.
8. 세그먼트 분석 결과 시각화하기
#날짜별로 단말기별 유저수 산출하기
> library(plyr)
> dau.user.info.device.summary<-ddply(dau.user.info,
.(log_date,device_type),
summarize,
dau=length(user_id))
> head(dau.user.info.device.summary)
log_date device_type dau
1 2013-08-01 Android 1784
2 2013-08-01 iOS 1805
3 2013-08-02 Android 1386
4 2013-08-02 iOS 1451
5 2013-08-03 Android 1295
6 2013-08-03 iOS 1351
#날짜별 데이터 형식으로 변환하기
> str(dau.user.info.device.summary)
'data.frame': 122 obs. of 3 variables:
$ log_date : chr "2013-08-01" "2013-08-01" "2013-08-02" "2013-08-02" ...
$ device_type: chr "Android" "iOS" "Android" "iOS" ...
$ dau : int 1784 1805 1386 1451 1295 1351 1283 1314 2002 2038 ...
> dau.user.info.device.summary$log_date<-as.Date(dau.user.info.device.summary$log_date)
> str(dau.user.info.device.summary)
'data.frame': 122 obs. of 3 variables:
$ log_date : Date, format: "2013-08-01" "2013-08-01" "2013-08-02" ...
$ device_type: chr "Android" "iOS" "Android" "iOS" ...
$ dau : int 1784 1805 1386 1451 1295 1351 1283 1314 2002 2038 ...
9. 시계열 트렌드 그래프 그리기
> 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)
-> 9월부터 안드로이드 유저가 빠르게 감소함을 알 수 있다. 따라서 안드로이드 시스템을 점검해야 한다는 결론을 얻을 수 있다.
#ggplot 그래프 구조 이해하기
> library(ggplot2)
> library(scales)
> head(dau.user.info.device.summary)
log_date device_type dau
1 2013-08-01 Android 1784
2 2013-08-01 iOS 1805
3 2013-08-02 Android 1386
4 2013-08-02 iOS 1451
5 2013-08-03 Android 1295
6 2013-08-03 iOS 1351
> ggplot(dau.user.info.device.summary)
> ggplot(dau.user.info.device.summary,aes(x=log_date,y=dau,col=device_type,lty=device_type,shape=device_type))
> 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)
> 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)
> 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<-c(0,max(dau.user.info.device.summary$dau))
> max(dau.user.info.device.summary$dau)
[1] 2133
> 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)
출처: 비지니스 활용 사레로 배우는 데이터 분석: R (사카마키 류지, 사토 요헤이 지음)
'Python, R 분석과 프로그래밍 > 비지니스 활용 사례로 배우는 데이터 분석 : R' 카테고리의 다른 글
6장 - 중회귀분석 : 집객효과가 가장 큰 광고의 조합은 무엇인가? (2) | 2017.02.15 |
---|---|
5장 - A/B 테스트 : 어느 쪽의 배너광고가 반응이 더 좋은가? (0) | 2017.02.13 |