forked from michalbrys/R-Google-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_users_segmentation.R
More file actions
executable file
·39 lines (30 loc) · 1.09 KB
/
5_users_segmentation.R
File metadata and controls
executable file
·39 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# K-Means Cluster Analysis
# load data into R
# you can download data from Google Analytics API or download sample dataset
# source('ga-connection.R')
# download and preview sample dataset
download.file(url="https://raw.githubusercontent.com/michalbrys/R/master/users-segmentation/sample-users.csv",
"sample-users.csv",
method="curl")
gadata <- read.csv(file="sample-users.csv", header=T, row.names = 1)
head(gadata)
# clustering users in 3 groups
fit <- kmeans(gadata, 3)
# get cluster means
aggregate(gadata,by=list(fit$cluster),FUN=mean)
# append and preview cluster assignment
clustered_users <- data.frame(gadata, fit$cluster)
head(clustered_users)
# visualize results in 3D chart
#install.packages("plotly")
library(plotly)
plot_ly(clustered_users,
x = clustered_users$beginner_pv,
y = clustered_users$intermediate_pv,
z = clustered_users$advanced_pv,
type = "scatter3d",
mode = "markers",
color=factor(clustered_users$fit.cluster)
)
# write results to file
write.csv(clustered_users, "clustered-users.csv", row.names=T)