Name: Subhankar Pattnaik ID: 71710059 Place: Hyderabad

Lets take the text file having locations shared by the users on watsapp (to undergo cleaning, refining and structuring process)

Load the required libraries

require(gsubfn)||install.packages("gsubfn")
## Loading required package: gsubfn
## Warning: package 'gsubfn' was built under R version 3.3.3
## Loading required package: proto
## [1] TRUE
require(ggmap)||install.packages("ggmap")
## Loading required package: ggmap
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.3
## [1] TRUE
require(plotGoogleMaps) || install.packages("plotGoogleMaps")
## Loading required package: plotGoogleMaps
## Loading required package: sp
## Loading required package: spacetime
## [1] TRUE
library(stringr)
library(gsubfn)
library(ggmap)
library(plotGoogleMaps)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date

Set the Working directory and read the file to be analysed from the diectory

setwd('Y:\\Knowledge Repository\\ISB\\DC\\Watsapp Group')
InputFile <- "WhatsApp Chat with DC Assign.txt"

Declare the variables to be used

leftOverText <- NULL
count <- 0
n <- 1
row <- NULL
time <- NULL
df = data.frame(NULL)

The below code segregates the data received from Watsapp ‘.txt’ file into Sender, Time, and Text & stores it in the List

conn <- file(InputFile, open = "r")
while(TRUE){
  line <- readLines(conn, n = 1, warn = FALSE)
  if ( length(line) == 0 ) {
    break
  }
  col <- unlist(str_split(line,"\\-",n=2))
  time <- as.POSIXct(strptime(col[[1]][1], "%d/%m/%Y, %H:%M", tz = "GMT"))
  if(is.na(time)){  
    leftOverText$Text[count] <- paste(leftOverText$Text[count], col[[1]][1], sep=" ")
  } else {
    count <- count + 1
    time <- col[[1]][1]
    name_text <- unlist(str_split(col[[2]],"\\:",n=2))
    
    row$Sender[count] <- name_text[1]
    row$Time[count] <- time
    row$Text[count] <- name_text[2]
    leftOverText$Text[count] <- ""
  }
}
close(conn)
row$Text <- paste(row$Text, leftOverText$Text, sep=" ") # Append the texts

Trim the text from the list

row$Text <- gsub("^\\s+|\\s+$", "", row$Text)

Function to extract Latitude, Longitude and Location from the Text (i.e. row$Text)

getLatLongLoc <- function() {
  patt <- "maps.google.com/\\?q="
  loc <- (strsplit(row$Text, patt))
  for(i in (row$Text)){
    if(grepl(patt, i)){
      location = strsplit(loc[n][[1]][2], ",")
      
      Latitude <- location[[1]][1]
      Longitude <- location[[1]][2]
      Location <- as.character(row$Text[n + 1])
      dfRow = data.frame("Sender" = row$Sender[n], "Time" = row$Time[n], Latitude, Longitude, Location, stringsAsFactors = F)
      df = rbind(df,dfRow)
    }
    n <- n + 1
  }
  return(df)
}

Call the function to retrieve Latitude and Longitude and store it in the required Data frame

df <- getLatLongLoc()
df$Sender <- gsub("^\\s+|\\s+$", "", df$Sender) # Trim the text
df$Latitude <- as.numeric(as.character(df$Latitude)) # Convert latitude to numeric
df$Longitude <- as.numeric(as.character(df$Longitude)) # Convert longitude to numeric

Plot the data frame having Latitudes and Longitudes of the users on Google Map

Plot the retrieved latitude and longitude in google map for filtered users

subhankar.location = df[df$Sender == "Subhankar ~ Rishi",]
map <- get_map(location = "Gachibowli, Hyderbad", zoom = 12, scale = 2)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Gachibowli,+Hyderbad&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gachibowli,%20Hyderbad&sensor=false
ggmap(map) +
  geom_point(data = subhankar.location, aes(x = Longitude, y = Latitude, fill = Location, alpha = 0.8), size = 5, shape = 21) +
  guides(alpha=FALSE, size=FALSE)

The below can be used to zoom in/out or move the google map having users location been plotted

coordinates(subhankar.location) <-~ Longitude +Latitude      # Create cordinates
proj4string(subhankar.location) = CRS('+proj=longlat +datum=WGS84')  # Add Projections in co-ord ref system
mcGoogleMaps(subhankar.location, zcol = "Location", mapTypeId='ROADMAP', filename='ZoomInOut.htm') # Plot on Google maps
## $starthtm
## [1] "<!DOCTYPE html> \n <html> \n <head> \n <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />\n               <meta charset=\"utf-8\"> \n <style type=\"text/css\">  \n html { height: 100% ; font-size: small} \n body { height: 100%; margin: 0px; padding: 0px }\n                #map_canvas {min-height: 100%;height:auto; } \n #cBoxes {position:absolute;right:5px; top:50px; background:white}\n               </style> \n\n               <script type=\"text/javascript\" src=\"https://maps.google.com/maps/api/js?sensor=false&v=3.18\"> </script>  \n\n               <script type=\"text/javascript\" src=\"https://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0.2/src/markerclusterer_compiled.js\"> </script>  \n\n               <script language=\"javascript\"> \n  \n USGSOverlay.prototype = new google.maps.OverlayView(); \nfunction USGSOverlay(bounds, image, map) {\n      this.bounds_ = bounds;\n      this.image_ = image;\n      this.map_ = map;\n      this.div_ = null;\n      this.setMap(map); }\nUSGSOverlay.prototype.onAdd = function() {\n      var div = document.createElement(\"DIV\");\n      div.style.border = \"none\";\n      div.style.borderWidth = \"0px\";\n      div.style.position = \"absolute\";\n      var img = document.createElement(\"img\");\n      img.src = this.image_;\n      img.style.width = \"100%\";\n      img.style.height = \"100%\";\n      div.appendChild(img);\n      this.div_ = div;\n      this.div_.style.opacity = 0.5;\n      var panes = this.getPanes();\n      panes.overlayImage.appendChild(this.div_);}\nUSGSOverlay.prototype.draw = function() {\n        var overlayProjection = this.getProjection();\n        var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());\n        var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());\n        var div = this.div_;\n        div.style.left = sw.x + \"px\";\n        div.style.top = ne.y + \"px\";\n        div.style.width = (ne.x - sw.x) + \"px\";\n        div.style.height = (sw.y - ne.y) + \"px\";} \nUSGSOverlay.prototype.onRemove = function() { \n this.div_.parentNode.removeChild(this.div_);} \nUSGSOverlay.prototype.hide = function() { if (this.div_) { this.div_.style.visibility = \"hidden\";} } \nUSGSOverlay.prototype.show = function() {if (this.div_) {  this.div_.style.visibility = \"visible\";}} \n       USGSOverlay.prototype.toggle = function() { \n if (this.div_) { \n  if (this.div_.style.visibility == \"hidden\") {  \n   this.show(); \n  } else { \n  this.hide(); } } } \n USGSOverlay.prototype.toggleDOM = function() {\n          if (this.getMap()) {\n            this.setMap(null);\n          } else {\n            this.setMap(this.map_);}}\n function setOpacR(Raster,textname) { \n  opac=0.01*parseInt(document.getElementById(textname).value) \n    Raster.div_.style.opacity= opac } \n"
## 
## $var
## [1] " var marker \n var  map  \n var  markerssubhankarxlocation1049 =[] ; var  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.4408461,78.3607904),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 05/03/17, 17:35  \\rLocation : Restaurant \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|C35527|10|b|Restaurant\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.419339,78.337915),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 07/03/17, 09:30  \\rLocation : Office \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|E17D1E|10|b|Office\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.4458893,78.355822),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 08/03/17, 22:45  \\rLocation : Home \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|9AE500|10|b|Home\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.418883,78.337107),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 09/03/17, 10:15  \\rLocation : Office \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|E17D1E|10|b|Office\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.417998,78.337901),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 10/03/17, 10:29  \\rLocation : Office \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|E17D1E|10|b|Office\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.445941,78.355843),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 10/03/17, 21:13  \\rLocation : Home \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|9AE500|10|b|Home\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.462911,78.367335),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 11/03/17, 13:10  \\rLocation : Restaurant \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|C35527|10|b|Restaurant\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.452247,78.363081),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 11/03/17, 17:22  \\rLocation : Grocery \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|00FF00|10|b|Grocery\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.495205,78.353591),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 12/03/17, 11:53  \\rLocation : Theatre \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|A52A2A|10|b|Theatre\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.433512,78.386501),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 12/03/17, 18:31  \\rLocation : Mall \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|FFA500|10|b|Mall\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.441284,78.358833),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 12/03/17, 20:17  \\rLocation : Restaurant \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|C35527|10|b|Restaurant\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.4350006,78.3409996),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 13/03/17, 13:12  \\rLocation : ISB college \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|D3C800|10|b|ISB college\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.441688,78.359544),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 13/03/17, 20:44  \\rLocation : Grocery \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|00FF00|10|b|Grocery\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.426847,78.341076),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 14/03/17, 16:26  \\rLocation : Office \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|E17D1E|10|b|Office\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.443046,78.357443),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 15/03/17, 21:19  \\rLocation : Grocery \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|00FF00|10|b|Grocery\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.436617,78.34731),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 16/03/17, 19:10  \\rLocation : Office \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|E17D1E|10|b|Office\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.4457929,78.3558902),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 17/03/17, 20:42  \\rLocation : Home \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|9AE500|10|b|Home\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n\nvar  marker = new google.maps.Marker({ \n position: new google.maps.LatLng(17.443874,78.358731),\n map:map,\n title:\"Sender : Subhankar ~ Rishi \\rTime : 17/03/17, 22:08  \\rLocation : Restaurant \\r\",\n clickable: true,\n draggable: false,\n flat: true,\n visible: true,\n icon: new google.maps.MarkerImage(\"http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|C35527|10|b|Restaurant\"), \n  zIndex: null }); \nmarkerssubhankarxlocation1049.push(marker); \n"
## 
## $functions
## [1] "function showR(R,boxname, map) {\n  R.setMap(map);\n  document.getElementById(boxname).checked = true; }\n  \n  function hideR(R,boxname) {\n  R.setMap(null);\n  document.getElementById(boxname).checked = false; }\n  \n  function showO(MLPArray,boxname, map ) { \n  for (var i = 0; i < MLPArray.length; i++) { \n  MLPArray[i].setMap(map); } \n  document.getElementById(boxname).checked = true; }\n  \n  function hideO(MLPArray,boxname) { \n  for (var i = 0; i < MLPArray.length; i++) { \n  MLPArray[i].setMap(null);} \n  document.getElementById(boxname).checked = false; } \n  \n  function boxclick(box,MLPArray,boxname, map) { \n  if (box.checked) { showO(MLPArray,boxname, map); \n  }else {  hideO(MLPArray,boxname);} }\n  \n  function setOpac(MLPArray,textname){\n  opacity=0.01*parseInt(document.getElementById(textname).value) \n  for(var i = 0; i < MLPArray.length; i++) {\n  MLPArray[i].setOptions({strokeOpacity: opacity, fillOpacity: opacity}); }}\n  \n  function setOpacL(MLPArray,textname) {\n  opacity=0.01*parseInt(document.getElementById(textname).value) \n  for (var i = 0; i < MLPArray.length; i++) {\n  MLPArray[i].setOptions({strokeOpacity: opacity});}}\n  \n  function setLineWeight(MLPArray,textnameW){\n  weight=parseInt(document.getElementById(textnameW).value)\n  for (var i = 0; i < MLPArray.length; i++){\n  MLPArray[i].setOptions({strokeWeight: weight}); } }\n  \n  function legendDisplay(box,divLegendImage){\n  element = document.getElementById(divLegendImage).style;\n  if (box.checked){ element.display=\"block\";} else {  element.display=\"none\";}}\n  \n  function boxclickR(box,R,boxname, map) {\n  if (box.checked){\n  showR(R,boxname,map); } else { hideR(R,boxname);} }\n  \n  function legendDisplay(box,divLegendImage){\n  element = document.getElementById(divLegendImage).style; \n  if (box.checked){ element.display=\"block\";} else {  element.display=\"none\";}}  \n function initialize() { \n var latlng = new google.maps.LatLng( 17.4566015 , 78.361804 ) ; \n \n var myOptions = { zoom: 15 , \n center: latlng , \n mapTypeId: google.maps.MapTypeId.ROADMAP  ,\n disableDefaultUI: false  ,\n disableDoubleClickZoom: false  ,\n  draggable: true  ,\n  keyboardShortcuts:  true  ,\n mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DEFAULT}  ,\n  navigationControl: true  ,\n navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT}  ,\n noClear: false  ,\n scaleControl: true  ,\n scaleControlOptions: {style: google.maps.ScaleControlStyle.STANDARD}  ,\n  scrollwheel: true  ,\n streetViewControl: false } ; \n \n map= new google.maps.Map(document.getElementById(\"map_canvas\"),myOptions); \n map.fitBounds(new google.maps.LatLngBounds(\n new google.maps.LatLng(17.417998,78.337107),\n new google.maps.LatLng( 17.495205,78.386501)));   var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[0] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 05/03/17, 17:35  <br>Location : Restaurant <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[0] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[1] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 07/03/17, 09:30  <br>Location : Office <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[1] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[2] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 08/03/17, 22:45  <br>Location : Home <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[2] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[3] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 09/03/17, 10:15  <br>Location : Office <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[3] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[4] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 10/03/17, 10:29  <br>Location : Office <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[4] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[5] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 10/03/17, 21:13  <br>Location : Home <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[5] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[6] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 11/03/17, 13:10  <br>Location : Restaurant <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[6] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[7] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 11/03/17, 17:22  <br>Location : Grocery <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[7] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[8] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 12/03/17, 11:53  <br>Location : Theatre <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[8] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[9] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 12/03/17, 18:31  <br>Location : Mall <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[9] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[10] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 12/03/17, 20:17  <br>Location : Restaurant <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[10] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[11] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 13/03/17, 13:12  <br>Location : ISB college <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[11] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[12] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 13/03/17, 20:44  <br>Location : Grocery <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[12] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[13] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 14/03/17, 16:26  <br>Location : Office <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[13] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[14] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 15/03/17, 21:19  <br>Location : Grocery <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[14] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[15] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 16/03/17, 19:10  <br>Location : Office <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[15] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[16] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 17/03/17, 20:42  <br>Location : Home <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[16] )});   \n\n  var infowindow = new google.maps.InfoWindow({ content: \"\", disableAutoPan:false, maxWidth :330, pixelOffset :null  }); \n google.maps.event.addListener(markerssubhankarxlocation1049[17] ,\"click\",function(event){ \n\n infowindow.setContent(\"Sender : Subhankar ~ Rishi <br>Time : 17/03/17, 22:08  <br>Location : Restaurant <br>\"); \n\n infowindow.setPosition(event.latLng);\ninfowindow.open(map,markerssubhankarxlocation1049[17] )});   \nshowO(markerssubhankarxlocation1049,\"markerssubhankarxlocation1049box\",map);  google.maps.event.addListener(  map , 'rightclick', function(event) {\n    var lat = event.latLng.lat();\n    var lng = event.latLng.lng();\n                              alert('Lat=' + lat + '; Lng=' + lng);});  var markerCluster = new MarkerClusterer( map , markerssubhankarxlocation1049   );  \n }"
## 
## $endhtm
## [1] "</script> \n </head> \n <body onload=\"initialize()\"> \n  <div id=\"map_canvas\"></div>  \n\n                           \n <div id=\"cBoxes\"> \n<input type=\"checkbox\" id=\"markerssubhankarxlocation1049box\" onClick='boxclick(this,markerssubhankarxlocation1049,\"markerssubhankarxlocation1049box\",map);' /> <b>subhankarxlocation<b> <hr /> \n <table> <tr>  <td> <input type=\"checkbox\"  checked=\"checked\" id=\"boxLegend308441dd4f32\" onClick='legendDisplay(this,\"Legend308441dd4f32\");' /> LEGEND </td> </tr>  <tr> <td>Location</td> </tr>\n                                    <tr> <td> <div style=\"display:block;\" id=\"Legend308441dd4f32\"> <img src=\"Legend308441dd4f32.png\" alt=\"Legend\" height=\"70%\"> </div>\n                           </td> </tr> \n </table> \n  <hr> \n </div> \n </body>  \n  </html>"

Lets analyze the user’s location pattern from the data frame

Structure and dimension of the data frame

str(subhankar.location)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  18 obs. of  3 variables:
##   .. ..$ Sender  : chr [1:18] "Subhankar ~ Rishi" "Subhankar ~ Rishi" "Subhankar ~ Rishi" "Subhankar ~ Rishi" ...
##   .. ..$ Time    : chr [1:18] "05/03/17, 17:35 " "07/03/17, 09:30 " "08/03/17, 22:45 " "09/03/17, 10:15 " ...
##   .. ..$ Location: chr [1:18] "Restaurant" "Office" "Home" "Office" ...
##   ..@ coords.nrs : int [1:2] 4 3
##   ..@ coords     : num [1:18, 1:2] 78.4 78.3 78.4 78.3 78.3 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:18] "2" "27" "79" "89" ...
##   .. .. ..$ : chr [1:2] "Longitude" "Latitude"
##   ..@ bbox       : num [1:2, 1:2] 78.3 17.4 78.4 17.5
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:2] "Longitude" "Latitude"
##   .. .. ..$ : chr [1:2] "min" "max"
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
##   .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
dim(subhankar.location) # Displays the dimension of data frame
## [1] 18  3
head(subhankar.location) # Shows few head rows of the data frame
## class       : SpatialPointsDataFrame 
## features    : 6 
## extent      : 78.33711, 78.36079, 17.418, 17.44594  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## variables   : 3
## names       :            Sender,             Time,   Location 
## min values  : Subhankar ~ Rishi, 05/03/17, 17:35 ,       Home 
## max values  : Subhankar ~ Rishi, 10/03/17, 21:13 , Restaurant

Data is collected for 12 different days. 6 were collected on weekend and weekday each

length(levels(as.factor((as.Date(subhankar.location$Time, "%d/%m/%y")))))
## [1] 12
day <- weekdays(as.Date(subhankar.location$Time, "%d/%m/%y"))
length(as.list(day)[day == "Saturday" | day == "Sunday"])
## [1] 6
subhankar.location$Day = day

7 unique places were visited

length(levels(as.factor(subhankar.location$Location)))
## [1] 7

The most frequently visited places were

table(subhankar.location$Location)[order(table(subhankar.location$Location), decreasing = TRUE)]
## 
##      Office  Restaurant     Grocery        Home ISB college        Mall 
##           5           4           3           3           1           1 
##     Theatre 
##           1

From the below data it shows that I have visited office only on weekdays

subhankar.location[subhankar.location$Location == "Office", -ncol(subhankar.location)]
##                Sender             Time Latitude Longitude Location
## 27  Subhankar ~ Rishi 07/03/17, 09:30  17.41934  78.33791   Office
## 89  Subhankar ~ Rishi 09/03/17, 10:15  17.41888  78.33711   Office
## 116 Subhankar ~ Rishi 10/03/17, 10:29  17.41800  78.33790   Office
## 292 Subhankar ~ Rishi 14/03/17, 16:26  17.42685  78.34108   Office
## 422 Subhankar ~ Rishi 16/03/17, 19:10  17.43662  78.34731   Office

On the weekends I have visited sites like Mall, Theatre and Restaurants etc. for the relaxation

subhankar.location[subhankar.location$Day == "Saturday" | subhankar.location$Day == "Sunday", -ncol(subhankar.location)]
##                Sender             Time Latitude Longitude   Location
## 2   Subhankar ~ Rishi 05/03/17, 17:35  17.44085  78.36079 Restaurant
## 159 Subhankar ~ Rishi 11/03/17, 13:10  17.46291  78.36733 Restaurant
## 164 Subhankar ~ Rishi 11/03/17, 17:22  17.45225  78.36308    Grocery
## 197 Subhankar ~ Rishi 12/03/17, 11:53  17.49520  78.35359    Theatre
## 208 Subhankar ~ Rishi 12/03/17, 18:31  17.43351  78.38650       Mall
## 218 Subhankar ~ Rishi 12/03/17, 20:17  17.44128  78.35883 Restaurant

Below is the plot of locations visited on weekdays

dfWday <- subhankar.location[subhankar.location$Day != "Saturday" & subhankar.location$Day != "Sunday", -ncol(subhankar.location)]
ggmap(map) +
  geom_point(data = dfWday, aes(x = Longitude, y = Latitude, fill = Location, alpha = 0.8), size = 5, shape = 21) +
  guides(alpha=FALSE, size=FALSE)

Below is the plot of locations visited on Weekends

dfWknd <- subhankar.location[subhankar.location$Day == "Saturday" | subhankar.location$Day == "Sunday", -ncol(subhankar.location)]
ggmap(map) +
  geom_point(data = dfWknd, aes(x = Longitude, y = Latitude, fill = Location, alpha = 0.8), size = 5, shape = 21) +
  guides(alpha=FALSE, size=FALSE)

I have visited grocery stores on one weekend and on the weekdays, as per the timestamp I have visited only after office hours

subhankar.location[subhankar.location$Location == "Grocery",-ncol(subhankar.location)]
##                Sender             Time Latitude Longitude Location
## 164 Subhankar ~ Rishi 11/03/17, 17:22  17.45225  78.36308  Grocery
## 242 Subhankar ~ Rishi 13/03/17, 20:44  17.44169  78.35954  Grocery
## 388 Subhankar ~ Rishi 15/03/17, 21:19  17.44305  78.35744  Grocery

As my office timings are from 9AM to 7PM, therefore from the below data it shows that after office I visit retaurants, grocery stores or I go to home directly.

t <- hour(as.POSIXct(strptime(subhankar.location$Time, "%d/%m/%Y, %H:%M", tz = "GMT")))
subhankar.location[t<9 | t>19,-ncol(subhankar.location)]
##                Sender             Time Latitude Longitude   Location
## 79  Subhankar ~ Rishi 08/03/17, 22:45  17.44589  78.35582       Home
## 153 Subhankar ~ Rishi 10/03/17, 21:13  17.44594  78.35584       Home
## 218 Subhankar ~ Rishi 12/03/17, 20:17  17.44128  78.35883 Restaurant
## 242 Subhankar ~ Rishi 13/03/17, 20:44  17.44169  78.35954    Grocery
## 388 Subhankar ~ Rishi 15/03/17, 21:19  17.44305  78.35744    Grocery
## 485 Subhankar ~ Rishi 17/03/17, 20:42  17.44579  78.35589       Home
## 490 Subhankar ~ Rishi 17/03/17, 22:08  17.44387  78.35873 Restaurant

There is only one outlier from the dataset, I have been to college sometime during office time which was actually I had taken a day off.

The other thing we can notice is on weekends I visited the places like restaurants, mall, theatre etc which are quite far from home but on the other hand I didn’t prefer to go on weekdays.