<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/"></div></div><div><a data-url="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/" data-counturl="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/" data-count="horizontal" data-text="Geographic data to service the needs of a remote employee – part2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mango-solutions.com/wp/2016/07/geographic-data-to-service-the-needs-of-a-remote-employee-part2/"> Mango Solutions » R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong><em>Ava Yang, Mango Solutions</em></strong></p>
<p><strong>Recap</strong></p>
<p>In <a title="Geographic data to service the needs of a remote employee - part1" target="_blank" href="http://www.mango-solutions.com/wp/?p=5693">part 1</a> of this post I set out to find a flat to rent based on three simple criteria:</p>
<ul>
<li>Café density</li>
<li>Tube station density</li>
<li>Monthly rent</li>
</ul>
<p>So far I have made use of the <a target="_blank" href="https://github.com/badbye/baidumap">baidumap</a> and <a target="_blank" href="https://github.com/lchiffon/REmap">REmap</a> packages to create a nice visualisation of available flats and coffee shops in Shanghai.</p>
<p><strong>Calculation and scoring</strong></p>
<p>Now let’s do some basic math and programming. Three measures ( derived from original variables to quantify my preferences.</p>
<p>For density of café and tube station, the closer the better; the more the better. Geographic distances were calculated by function distm from package <a target="_blank" href="https://cran.r-project.org/web/packages/geosphere/index.html">geosphere</a>.</p>
<pre><code>
library(dplyr)
library(geosphere)
library(knitr)
library(baidumap)

load('data/ziroom.rds') # raw data
load(&quot;data/sh_cafe.rds&quot;)
load(&quot;data/sh_station.rds&quot;)

# 1. Generate names to represent flats
# 2. Extract longitude and lattitude
sh_ziroom &lt;- ziroom %>%
mutate(name=paste(&quot;Room&quot;, rownames(ziroom), sep=&quot;_&quot;)) %>%
mutate(lon=getCoordinate(flat, city=&quot;上海&quot;, formatted = T)[, 'longtitude']) %>%
mutate(lat=getCoordinate(flat, city=&quot;上海&quot;, formatted = T)[, 'latitude']) %>%
na.omit() %>%
select(c(lon, lat, name, price_promotion, flat))

# distance matrices: between cafe and flat, between station and flat

dist_cafe_flat &lt;- distm(sh_cafe[,c(&quot;lon&quot;, &quot;lat&quot;)], sh_ziroom[,c(&quot;lon&quot;, &quot;lat&quot;)]) %>%

as.data.frame()

dist_station_flat &lt;- distm(sh_station[,c(&quot;lon&quot;, &quot;lat&quot;)], sh_ziroom[,c(&quot;lon&quot;, &quot;lat&quot;)]) %>%

as.data.frame()
</code></pre>
<p>As an upper limit I’m willing to walk as far as 750 metres (about 0.5 mile) from a café. Thus, cafe<sub>idx</sub> and station<sub>idx</sub> were then given by</p>
<p>For this job I wrote a small custom function called calIdx.</p>
<pre><code>
# Function to calculate cafe_idx and station_idx

calIdx &lt;- function(tmpcol) {

tmpcol &lt;- tmpcol[which(tmpcol &lt; 750)]

return(sum(1/log(tmpcol)))

}
</code></pre>
<p>Rent is a negative indicator, and so rent<sub>idx</sub> could be obtained from</p>
<p>The weighted score was calculated by</p>
<pre><code>
# 1. cafeIdx = 1/log(dis1) + 1/log(dis2) +...+ 1/log(disN)

# 2. stationIdx = 1/log(dis1) + 1/log(dis2) +...+ 1/log(disN)

# 3. rentIdx = 1/log(price_promotion)

# 4. score = 0.3*cafeIdx + 0.2*stationIdx + 0.5*rentIdx

sh_ziroom_top10 &lt;- sh_ziroom %>%

mutate(cafeIdx = sapply(dist_cafe_flat, calIdx)) %>%

mutate(stationIdx = sapply(dist_station_flat, calIdx)) %>%

filter(price_promotion &lt;= 4000) %>%

mutate(rentIdx = 1/log(as.numeric(price_promotion))) %>%

mutate(score = 0.4*cafeIdx + 0.2*stationIdx + 0.4*rentIdx) %>%

arrange(desc(score)) %>%

slice(1:10)
</code></pre>
<p><strong>Summary</strong></p>
<pre><code>
kable(sh_ziroom_top10[, c(&quot;name&quot;, &quot;score&quot;, &quot;cafeIdx&quot;, &quot;stationIdx&quot;, &quot;rentIdx&quot;)], align=&quot;c&quot;)

 
<table width="100%">
<tbody>
<tr>
<th>name</th>
<th>score</th>
<th>cafeIdx</th>
<th>stationIdx</th>
<th>rentIdx</th>
</tr>
<tr>
<td>Room_34</td>
<td>0.6480966</td>
<td>1.3262957</td>
<td>0.3380904</td>
<td>0.1249006</td>
</tr>
<tr>
<td>Room_35</td>
<td>0.6470510</td>
<td>1.3262957</td>
<td>0.3380904</td>
<td>0.1222865</td>
</tr>
<tr>
<td>Room_80</td>
<td>0.6054141</td>
<td>1.2216344</td>
<td>0.3378458</td>
<td>0.1229781</td>
</tr>
<tr>
<td>Room_79</td>
<td>0.6048128</td>
<td>1.2216344</td>
<td>0.3378458</td>
<td>0.1214746</td>
</tr>
<tr>
<td>Room_22</td>
<td>0.5729428</td>
<td>1.1430015</td>
<td>0.3349634</td>
<td>0.1218737</td>
</tr>
<tr>
<td>Room_24</td>
<td>0.5729428</td>
<td>1.1430015</td>
<td>0.3349634</td>
<td>0.1218737</td>
</tr>
<tr>
<td>Room_45</td>
<td>0.5292036</td>
<td>0.9617378</td>
<td>0.4709076</td>
<td>0.1258173</td>
</tr>
<tr>
<td>Room_46</td>
<td>0.5284566</td>
<td>0.9617378</td>
<td>0.4709076</td>
<td>0.1239499</td>
</tr>
<tr>
<td>Room_59</td>
<td>0.4334636</td>
<td>0.8012006</td>
<td>0.3237803</td>
<td>0.1205684</td>
</tr>
<tr>
<td>Room_57</td>
<td>0.3836545</td>
<td>0.6721137</td>
<td>0.3302977</td>
<td>0.1218737</td>
</tr>
</tbody>
</table>
</code></pre>
<p>Done! See above for the top 10 room candidates. The mechanism I used is not difficult and makes my life so much easier. Moving to a new area which fulfils all my social needs is no longer such a big challenge!</p>
<p> </p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/"></div></div><div><a data-url="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/" data-counturl="http://www.r-bloggers.com/geographic-data-to-service-the-needs-of-a-remote-employee-part2/" data-count="vertical" data-text="Geographic data to service the needs of a remote employee – part2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.mango-solutions.com/wp/2016/07/geographic-data-to-service-the-needs-of-a-remote-employee-part2/"> Mango Solutions » R Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/52op4cVSLMQ"></a></div>