library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
#set.seed(100)
store=data.frame(lat=runif(1,29,34),long=runif(1,69,74))
onlineloc=data.frame(lat=runif(10,29,34),long=runif(10,69,74))
instore=data.frame(lat=runif(5,29,34),long=runif(5,69,74))
instorecustomer=round(instore,digits = 2)
plot(onlineloc,main="Online and instore customer loc",col=" red")
points(instore,pch=2)
points(store,pch=15)
legend(x=30,y=68,legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))
#plot(onlineloc,main="Online customer loc",col=" red")
results=kmeans(onlineloc,5)
results
#> K-means clustering with 5 clusters of sizes 1, 3, 2, 2, 2
#>
#> Cluster means:
#> lat long
#> 1 29.02242 69.56644
#> 2 33.02029 73.36389
#> 3 32.45367 69.42030
#> 4 33.46060 71.63696
#> 5 30.57288 71.81424
#>
#> Clustering vector:
#> [1] 4 1 3 3 4 2 2 5 5 2
#>
#> Within cluster sum of squares by cluster:
#> [1] 0.00000000 1.21289514 0.01628100 0.00492958 1.40352742
#> (between_SS / total_SS = 94.3 %)
#>
#> Available components:
#>
#> [1] "cluster" "centers" "totss" "withinss"
#> [5] "tot.withinss" "betweenss" "size" "iter"
#> [9] "ifault"
onlinecluster=results$centers[,]
onlinecluster=round(onlinecluster,digits = 2)
distance2=dist(onlinecluster)
round(distance2,digits = 2)
#> 1 2 3 4
#> 2 5.51
#> 3 3.43 3.98
#> 4 4.90 1.78 2.44
#> 5 2.72 2.90 3.04 2.89
plot(hclust(distance2))
library(mapsapi)
library(gmapsdistance)
library(ggmap)
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
finalloc=paste0(LAT=onlinecluster[,1],"+",LONG=onlinecluster[,2])
startloc=paste0(LAT=instorecustomer[,1],"+",LONG=instorecustomer[,2])
d=mapdist(startloc,finalloc, mode = "driving",key="")
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=30.77+71.17&destinations=32.45+69.42&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=32.19+71.38&destinations=30.57+71.81&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.24+70.19&destinations=29.02+69.57&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.53+72.91&destinations=33.46+71.64&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.95+73.64&destinations=33.02+73.36&key=xxx&mode=driving
#> All columns in a tibble must be 1d or 2d objects:
#> * Column `m` is NULL
#> * Column `seconds` is NULL
d
#> Error in eval(expr, envir, enclos): object 'd' not found
Created on 2020-02-23 by the reprex package (v0.3.0)