<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/basic-tree-1-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/12/09/basic-tree-1/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/treeplanting.png?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="treeplanting" class="wp-image-3097" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/11/treeplanting-150x150.png?resize=150%2C150" data-recalc-dims="1"></a><br>
Using the knowledge you acquired in the previous exercises on sampling and selecting<a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/06/select-and-query-exercise/">(here)</a>, we will now go through an entire data analysis process. You will be using what you know as crutches to solve the problems. Don’t worry. It might look intimidating but follow the sequence and you will see that modeling a decision tree is the best decision you made today. We will take you through all stages of the data pipeline. From Data loading,feature selection, sampling, plotting, modelling and evaluating a decision tree.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/09/basic-tree-1-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>
Use read.csv() command to load the lenses.csv data and store it in lens. Use the str() command to see lens. Download the dataset from <a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/lenses.csv">here </a></p>
<p><strong>Exercise 2</strong><br>
Notice there are no column names. The column names are as follows<br>
index, age, spec_pres, astigmatic, tpr. Use one line code to change the column names to the aforementioned names.</p>
<p><strong>Exercise 3</strong><br>
Given the meta data</p>
<p> age: (1) young, (2) pre-presbyopic, (3) presbyopic<br>
spec_pres: (1) myope, (2) hypermetrope<br>
astigmatic: (1) no, (2) yes<br>
tpr: (1) reduced, (2) normal<br>
class: (1) patient needs hard contact lens, (2) patient needs soft contact lens, (3) patient does not need contact lens</p>
<p>Type the code <code>lens$age[lens$age == &quot;1&quot;]=&quot;young&quot; </code><br>
Use the same format to change all the data to its names for the age and spec_pres variables.</p>
<p><strong>Exercise 4</strong><br>
Use the <code>str()</code> command to see the changes. Also notice that the astigmatic column is a factor that is also storing numbers as characters. To get all of them in the same format, lets convert it to character. Use the code as.character() to convert this column data type to character.</p>
<p><strong>Exercise 5</strong><br>
Now change the astigmatic column data to the right names</p>
<p><strong>Exercise 6</strong><br>
Use the following code to replace the 1 with “reduced in the tpr column</p>
<p><code>lens$tpr[lens$tpr==1]=&quot;reduced&quot; </code></p>
<p>Now type str(lens) to see the dataframe. Notice that the tpr column data type change to character from integer. Anytime you introduce something that is not a number in a number dataframe, it will become a character.</p>
<p><strong>Exercise 7</strong><br>
Go ahead and replace 2 in the tpr column with “normal”</p>
<p><strong>Exercise 8</strong><br>
use the <code>table()</code> command to see the counts of each data type</p>
<p><strong>Exercise 9</strong><br>
Notice that there is a g in the count. That could possibly be a typo. We can go ahead and remove that row since there is only one row with that typo. Hint: You can select all rows that does not have that typo and store it back in the lens dataframe.</p>
<p><strong>Exercise 10</strong><br>
Great Work. We realized that the index column is not necessary for our modeling purposes. So lets remove the index column.</p>
<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/2016/12/09/basic-tree-1/"> 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/YLyhdXyZ-OY"></a></div>