<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/data-hacking-with-rdstk-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/02/16/data-hacking-with-rdstk-3/?utm_source=rss&utm_medium=rss&utm_campaign=data-hacking-with-rdstk-3"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i0.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAhrAAAAJDhhNDdhODU3LTI1OTEtNDkwYS1hOGI3LTliMzM2OTYyZWE2MA.jpg?resize=300%2C175&ssl=1" data-recalc-dims="1">RDSTK is a very versatile package. It includes functions to help you convert IP address to geo locations and derive statistics from them. It also allows you to input a body of text and convert it into sentiments. </p>
<p>This is a continuation from the last exercise <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/02/11/rdstk2/">RDSTK 2</a><br>
We are going to use the function that we created in our last exercise to have a programmatic way to derive statistics using the coordinates2statistics() function. Last week we talked about local and global variables. This is important to understand before proceeding. Also refresh on ip2coordinates() function.</p>
<p>This package provides an R interface to Pete Warden’s Data Science Toolkit. See for more information click <a target="_blank" rel="nofollow" href="http://www.datasciencetoolkit.org/">here</a>.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/02/16/data-hacking-with-rdstk-solution-3/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
This week we will give you bigger and badder list to work with. Its a list of more a dozen proxy ip-addresses from the internet. Run the code</p>
<p><code><br>
list=c(&quot;97.77.104.22&quot;,&quot;104.199.228.65&quot;,&quot;50.93.204.169&quot;,&quot;107.189.46.5&quot;,&quot;104.154.142.10&quot;,&quot;104.131.255.12&quot;,&quot;209.212.253.44&quot;,&quot;70.248.28.23&quot;,&quot;52.119.20.75&quot;,&quot;192.169.168.15&quot;,&quot;47.88.31.75	80&quot;,&quot;107.178.4.109&quot;,&quot;152.160.35.171&quot;,&quot;104.236.54.196&quot;,&quot;50.93.197.102&quot;,&quot;159.203.117.1&quot;,&quot;206.125.41.132&quot;,&quot;50.93.201.28&quot;,&quot;8.21.67.248	31&quot;,&quot;104.28.16.199&quot;)
<p></p>
<p><strong>Exercise 2</strong></p>
<p>Remember how we used iterators to run through each location and derive the stats with ip2coordinates() function in the first rdstk exercise. Lets do the same here. Store the results in df</p>
<p><strong>Exercise 3</strong></p>
<p>If you came this far, great. Lets recall the function that we created in exercise 2. If you do not remember the function, here is the code for it. Run the code below and then run stat_maker(“population_density”). You should see a new column called pop<br>
<code><br>
stat_maker=function(s2){<br>
   s1=&quot;statistics&quot;<br>
   s3=&quot;value&quot;<br>
   s2=as.character(s2)<br>
   for (i in 1:nrow(df)) {<br>
     df$pop[i] &lt;&lt;-coordinates2statistics(df[i,3],df[i,6],s2)[paste(s1,s2,s3, sep = &quot;.&quot;)]<br>
     assign(&quot;test2&quot;,50,envir = .GlobalEnv)
<p> }<br>
}</p>
<p></p>
<p>You should see an output in the format “statistics.hello.value”</p>
<p><strong>Exercise 4</strong></p>
<p>Modify the function so that the function accepts a string and returns out a global variable that holds the elements of that string statistic. For example if you input elevation, the function will create a global variable called elevation with the results from the for loop stored</p>
<p><strong>Exercise 5</strong></p>
<p>Test out the function. </p>
<p><code><br>
stat_maker(&quot;elevation&quot;)<br>
</code></p>
<p><strong>Exercise 6</strong></p>
<p>Test the function stat_maker. stat_maker(“population_density”). Notice it did not explicitly make the changes to the df but just returned it once you called the function. This is because we did not define df as a global variable. But thats okay. We will learn it later</p>
<p><strong>Exercise 7</strong></p>
<p>Great. Now before we modify our function, lets learn how we can make a global variable inside a function. Use the same code from exercise 5 but this time instead of defining df$pop2 as a local variable, define it as a global variable. Run the function and test it again.</p>
<p><strong>Exercise 8</strong></p>
<p>Run the code<br>
<code>
<p>stat_maker(&quot;us_population_poverty&quot;)<br>
</p>
<p>Notice that our function does not work for this case. This is because anything with the prefix us_population will return a dataframe with a column value like statistics.us_population.value<br>
So you need to modify the function a little to accomodate for this.</p>
<p><strong>Exercise 9</strong></p>
<p>Run the following commands. You can also use any string starting with us_population fo this function. But the goal is to make global variables that hold this data. You can refer to the whole list of statistic funtions at www.datasciencetoolkit.org </p>
<p><code>
<p>stat_maker(&quot;us_population&quot;)<br>
stat_maker(&quot;us_population_poverty&quot;)<br>
stat_maker(&quot;us_population_asian&quot;)<br>
stat_maker(&quot;us_population_bachelors_degree&quot;)<br>
stat_maker(&quot;us_population_black_or_african_american&quot;)<br>
stat_maker(&quot;us_population_black_or_african_american_not_hispanic &quot;)<br>
stat_maker(&quot;us_population_eighteen_to_twenty_four_years_old&quot;)<br>
stat_maker(&quot;us_population_five_to_seventeen_years_old&quot;)<br>
stat_maker(&quot;us_population_foreign_born&quot;)<br>
stat_maker(&quot;us_population_hispanic_or_latino&quot;)</p>
<p></p>
<p><strong>Exercise 10</strong></p>
<p>Use cbind command to bind all the global variables into the df. Print the results of df. </p>
<p>Note: You can chose to make this df in other ways but this method was used to guide through modifying functions, global/local variables and working with strings.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Data Hacking with RDSTK 2" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/02/11/rdstk2/">Data Hacking with RDSTK 2 </a></li>
<li><a title="Data Hacking with RDSTK (part 1)" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/31/rdstk1/">Data Hacking with RDSTK (part 1) </a></li>
<li><a title="Descriptive Analytics-Part 4 : Data Manipulation" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/11/21/descriptive-analytics-part-4-data-manipulation/">Descriptive Analytics-Part 4 : Data Manipulation </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>

<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2017/02/16/data-hacking-with-rdstk-3/?utm_source=rss&utm_medium=rss&utm_campaign=data-hacking-with-rdstk-3"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://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="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/rRosuWnF5ig"></a></div></code></code></code></code>