Skip to content

Commit afad9fc

Browse files
authored
Create OrderPerUserCohort.R
Calculates number of orders per user per cohort
1 parent 5d0a70b commit afad9fc

1 file changed

Lines changed: 172 additions & 0 deletions

File tree

OrderPerUserCohort.R

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
Sys.setenv(RSTUDIO_PANDOC="/usr/lib/rstudio-server/bin/pandoc")
2+
3+
#Set wd
4+
getwd()
5+
setwd("/home/bi_user/Automation/Reports/Cohorts")
6+
7+
rm(list = ls(all=TRUE))
8+
9+
#Provide new paths to libraries
10+
.libPaths("/home/bi_user/R/x86_64-pc-linux-gnu-library/3.3/")
11+
#Load libraries
12+
library(zoo)
13+
library(DT)
14+
library(data.table)
15+
library(dplyr)
16+
library(DT)
17+
library(plyr)
18+
library(htmlwidgets)
19+
20+
#Load data
21+
load("DataToCohorts.dat")
22+
23+
#Subset the dataframe
24+
dataset <- subset(orders_data, select = c(3,5:8,13,16))
25+
26+
#Convert createdAt to date
27+
dataset$createdAt <- as.Date(dataset$createdAt, format = "%Y-%m-%d")
28+
29+
#Create a datatable
30+
dataset = data.table(dataset)
31+
32+
#Create a dt with the min created (aka, first order), aggregation by customer
33+
newdf_firstorder <- dataset[isvalid == 1 , min(createdAt) , by = .(customer)]
34+
35+
#Merge back to dataset
36+
dataset <- merge(x = dataset, y= newdf_firstorder, by = "customer", all.x = TRUE)
37+
38+
#Rename the first order col
39+
colnames(dataset)[8] <- c("firstOrder")
40+
41+
#Get first order
42+
dataset$sinceFirstOrder <- 12 * as.numeric((as.yearmon(dataset$createdAt)-as.yearmon(dataset$firstOrder)))
43+
#Change to year-month format
44+
dataset$initial_cohort <- as.yearmon(dataset$firstOrder)
45+
46+
#Create Customer Status, i.e. convert to 0 if New and 1 to Returning
47+
dataset$BinCustomerStatus = 0
48+
dataset$BinCustomerStatus[which(dataset$createdAt > dataset$firstOrder)] = 1
49+
50+
#Subset dataset with completed state
51+
nsubset <- dataset[dataset$state == 'completed']
52+
53+
54+
#Check the count of returning customers, note "by" function: aggregate by BOTH sinceFirstOrder and initiacohort
55+
cohort = arrange(nsubset[BinCustomerStatus==1, .(sum(isvalid),length(unique(customer))),
56+
by=.(initial_cohort, sinceFirstOrder)],desc(sinceFirstOrder))
57+
colnames(cohort) <- c("initialcohort","sinceFirstOrder","orders","customers")
58+
59+
#Create percentages
60+
cohort$proportion <- round(cohort$orders /cohort$customers,2)
61+
#Subset the dataset, deleting two columns
62+
cohort$orders <- NULL
63+
cohort$customers <- NULL
64+
65+
#Employ the dcast library
66+
output <- arrange(dcast(cohort, initialcohort~round(sinceFirstOrder), value.var = "proportion"),initialcohort)
67+
output = data.table(output)
68+
69+
#Make intitial cohort character for displaying purposes
70+
output$initialcohort <- as.character(output$initialcohort)
71+
72+
73+
#Create output
74+
cohort_table <- datatable(output,extensions = list("Buttons"= NULL), options = list("paging"= F,"searching"=T,
75+
"autoWidth"= F, dom = 'Bfrtip',
76+
buttons = c( 'csv','excel')))
77+
78+
79+
saveWidget(cohort_table, file="/home/bi_user/shinyserver/Cohorts/OrdersPerUser_cohorts_global.html",selfcontained = F)
80+
81+
82+
##########################################################################################################
83+
#### NOW BY CITY
84+
##########################################################################################################
85+
86+
#Create a df from nsubset with regard to cities
87+
cohortByCities = arrange(nsubset[BinCustomerStatus==1, .(sum(isvalid),length(unique(customer))),
88+
by=.(initial_cohort, sinceFirstOrder,location)],desc(initial_cohort))
89+
90+
colnames(cohortByCities) <- c("initial_cohort","sinceFirstOrder","location","orders","customers")
91+
92+
cohortByCities <- data.table(cohortByCities)
93+
##################################
94+
#London
95+
##################################
96+
london <- cohortByCities[cohortByCities$location == "gb_london"]
97+
london <- na.omit(london)
98+
#Create percentages
99+
london$proportion <- round(london$orders / london$customers,2)
100+
#Subset the dataset, deleting two columns
101+
london$orders <- NULL
102+
london$customers <- NULL
103+
104+
105+
#Employ the dcast library
106+
lon_output <- arrange(dcast(london, initial_cohort~round(sinceFirstOrder), value.var = "proportion"), initial_cohort)
107+
lon_output = data.table(lon_output)
108+
109+
lon_output$initial_cohort <- as.character(lon_output$initial_cohort)
110+
#Create output
111+
lon_table <- datatable(lon_output,extensions = list("Buttons"= NULL), options = list("paging"= F,"searching"=T,
112+
"autoWidth"= F, dom = 'Bfrtip',
113+
buttons = c( 'csv','excel')))
114+
115+
saveWidget(lon_table, file="/home/bi_user/shinyserver/Cohorts/OrdersPerUser_cohorts_London.html",selfcontained = F)
116+
117+
118+
##################################
119+
#Berlin
120+
##################################
121+
berlin <- cohortByCities[cohortByCities$location == "de_berlin"]
122+
berlin <- na.omit(berlin)
123+
#Create percentages
124+
berlin$proportion <- round(berlin$orders / berlin$customers,2)
125+
#Subset the dataset, deleting two columns
126+
berlin$orders <- NULL
127+
berlin$customers <- NULL
128+
129+
#Subset everything after launch
130+
berlin <- berlin[berlin$initial_cohort >= "Jan 2015"]
131+
132+
#Employ the dcast library
133+
ber_output <- arrange(dcast(berlin, initial_cohort~round(sinceFirstOrder), value.var = "proportion"), initial_cohort)
134+
ber_output = data.table(ber_output)
135+
136+
137+
ber_output$initial_cohort <- as.character(ber_output$initial_cohort)
138+
#Create output
139+
ber_table <- datatable(ber_output,extensions = list("Buttons"= NULL), options = list("paging"= F,"searching"=T,
140+
"autoWidth"= F, dom = 'Bfrtip',
141+
buttons = c( 'csv','excel')))
142+
143+
saveWidget(ber_table, file="/home/bi_user/shinyserver/Cohorts/OrdersPerUser_cohorts_Berlin.html",selfcontained = F)
144+
145+
146+
##################################
147+
#Paris
148+
##################################
149+
paris <- cohortByCities[cohortByCities$location == "fr_paris"]
150+
paris <- na.omit(paris)
151+
#Create percentages
152+
paris$proportion <- round(paris$orders / paris$customers,2)
153+
#Subset the dataset, deleting two columns
154+
paris$orders <- NULL
155+
paris$customers <- NULL
156+
157+
#Subset everything after launch
158+
paris <- paris[paris$initial_cohort >= "May 2016"]
159+
160+
#Employ the dcast library
161+
par_output <- arrange(dcast(paris, initial_cohort~round(sinceFirstOrder), value.var = "proportion"), initial_cohort)
162+
par_output = data.table(par_output)
163+
164+
165+
par_output$initial_cohort <- as.character(par_output$initial_cohort)
166+
#Create output
167+
par_table <- datatable(par_output,extensions = list("Buttons"= NULL), options = list("paging"= F,"searching"=T,
168+
"autoWidth"= F, dom = 'Bfrtip',
169+
buttons = c( 'csv','excel')))
170+
171+
saveWidget(par_table, file="/home/bi_user/shinyserver/Cohorts/OrdersPerUser_cohorts_Paris.html",selfcontained = F)
172+

0 commit comments

Comments
 (0)