<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-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/02/11/rdstk2/?utm_source=rss&utm_medium=rss&utm_campaign=rdstk2"> 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=276%2C158&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/01/31/rdstk1/">RDSTK 1</a><br>
This package provides an R interface to Pete Warden’s Data Science Toolkit. See www.datasciencetoolkit.org for more information.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/02/11/data-hacking-with-rdstk-2/">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>
Load the rdstk2 csv dataset.</p>
<p><strong>Exercise 2</strong></p>
<p>a.create a string called s1 and store “statistics” inside<br>
b.create a string called s3 and store “value”<br>
c. create a function that will take a string s2 as an input and output a string in the format s1+s2+s3 seperated by “.”. Name this function “stringer”</p>
<p><strong>Exercise 3</strong></p>
<p>Lets test out this function.</p>
<p><code><br>
stringer(&quot;hello&quot;)<br>
</code></p>
<p>You should see an output in the format “statistics.hello.value”</p>
<p><strong>Exercise 4</strong></p>
<p>Create a for loop that will iterate over the rows in df and derive the population density of the location using coordinates2statistics function. Save the results in df$pop</p>
<p><strong>Exercise 5</strong></p>
<p>Lets now make a function using elements you learned from exercise 3 and 4. So the function is going to take a string as an input like s2 from exercise 3. Inside the function you can combine it with s1 and s3. You have to create the same for loop from exercise 4. Instead of storing the result of the for loop in df$pop, use df$pop2.You should see a new feature inside df with all the results once you return df from it.</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>You can also use the assign() function inside a function and set the results as a global variable. Lets see an example of assign function</p>
<p> </p>
<p>assign(“test”,50)</p>
<p> </p>
<p>Now if you type test in your console. You should see 50. Try it</p>
<p><strong>Exercise 9</strong></p>
<p>Now try putting the same code in exercise 8 while changing test to test2 inside the stat_maker function. Once you test the function, you will see that test2 does not return anything. This is because it was not set as a global variable</p>
<p><strong>Exercise 10</strong></p>
<p>Set test2 as a global variable inside the stat_maker function. Run the function and now you should see test 2 return 50 when you call it.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<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="Functions exercises vol. 2" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/05/functions-exercises-vol-2/">Functions exercises vol. 2 </a></li>
<li><a title="Scripting Loops In R" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/06/01/scripting-loops-in-r/">Scripting Loops In R </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/11/rdstk2/?utm_source=rss&utm_medium=rss&utm_campaign=rdstk2"> 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/tO5XaK0H8xQ"></a></div>