<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/accessing-dataframe-objects-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/accessing-dataframe-objects-exercises/" data-counturl="http://www.r-bloggers.com/accessing-dataframe-objects-exercises/" data-count="horizontal" data-text="Accessing Dataframe Objects Exercises" 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://r-exercises.com/2016/05/20/accessing-dataframe-objects-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/05/cooltext184292849437462.png"><img sizes="(max-width: 300px) 100vw, 300px" srcset="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/05/cooltext184292849437462-300x206.png?resize=300%2C206 300w, http://r-exercises.com/wp-content/uploads/2016/05/cooltext184292849437462.png 468w" alt="access dataframe" class="wp-image-782" src="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/05/cooltext184292849437462-300x206.png?resize=300%2C206" data-recalc-dims="1"></a><br>
The <code>attach()</code> function alters the R environment search path by making dataframe variables into global variables. If incorrectly scripted, the <code>attach()</code> function might create symantic errors. To prevent this possibility, <code>detach()</code> is needed to reset the dataframe objects in the search path.</p>
<p>The <code>transform()</code> function allows for transformation of dataframe objects. The <code>within()</code> function creates a new dataframe, when modifying dataframe variables.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/5/20/accessing-dataframe-objects-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
<code>attach()</code> – Attach a set of R Objects to Search Path</p>
<p>Required Dataframe:<code><br>
buildingSurvey &lt;- data.frame(name=c(&quot;bldg1&quot;, &quot;bldg2&quot;, &quot;bldg3&quot;,<br>
                                    &quot;bldg4&quot;, &quot;bldg5&quot;, &quot;bldg6&quot;),<br>
                        survey=c(1,1,1,2,2,2),<br>
                        location=c(1,2,3,2,3,1),<br>
                        floors=c(5, 10, 10, 11, 8, 12),<br>
                        efficiency=c(51,64,70,71,80,58))<br>
</code></p>
<p>Use the <code>attach()</code> function to make the variables in <code>&quot;buildingSurvey&quot;</code> independently searchable. Then, use “<code>summary(location)</code>” to create a summary of the “<code>floors</code>” variable.</p>
<p><strong>Exercise 2</strong><br>
Using the “<code>summary()</code>” function, find the median “<code>efficiency</code>” value of “<code>buildingSurvey</code>“, using objects in the R environment search path.</p>
<p><strong>Exercise 3</strong><br>
Once attached, in order to change the dataframe variable, use the assignment operator “<code>&lt;&lt;-</code>“. For example: <code>variable1 &lt;&lt;- log(variable1)</code></p>
<p>Use “<code>&lt;&lt;-</code>” to divide the “<code>efficiency</code>” category by <code>100</code>.</p>
<p><strong>Exercise 4</strong><br>
<code>detach()</code> – Detach Objects from the Search Path</p>
<p>After detaching, modified <code>attach()</code> dataframes are restored to their pre-<code>attach()</code> values. and the R environment search path is restored. <code>detach()</code> is needed to prevent symantec errors in programming.</p>
<p>Therefore, use the <code>detach()</code> function to restore the search paths of the dataframe, “<code>buildingSurvey</code>“.</p>
<p><strong>Exercise 5</strong><br>
The “<code>transform()</code>” function performs a transformation on a dataframe object.</p>
<p>Use <code>transform()</code> to replace the “<code>efficiency</code>” column’s values with the starting values divided by <code>100</code>.</p>
<p><strong>Exercise 6</strong><br>
First, re-attach the dataframe, “<code>buildingSurvey</code>“.</p>
<p>Then, use <code>transform()</code> to evaluate the log of the “<code>efficiency</code>” variable. Set the result to the dataframe, “<code>efficiencyL</code>“. The column names of the dataframe “<code>efficiencyL</code>” should be “<code>X_data</code>“, and “<code>efficiencyLog</code>“.</p>
<p><strong>Exercise 7</strong><br>
Next, use <code>transform()</code> to round the “<code>efficiencyLog</code>” variable of “<code>efficiencyL</code>” to one decimal place.</p>
<p><strong>Exercise 8</strong><br>
The <code>within()</code> function creates a modified copy of a dataframe.</p>
<p>For this exercise, use <code>within()</code> to append the “<code>buildingSurvey</code>” dataframe with a variable called, “<code>efficiency10</code>“. The new variable contains “<code>efficiency</code>” multiplied by <code>10</code>.</p>
<p><strong>Exercise 9</strong><br>
Use the <code>within()</code> function to set <code>efficiency[4]</code> to “<code>85</code>“. This will also create a copy of “<code>buildingSurvey</code>“. Setting a new dataframe isn’t required for this exercise.</p>
<p><strong>Exercise 10</strong><br>
For the final exercise, restore the R environment search path.</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/accessing-dataframe-objects-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/accessing-dataframe-objects-exercises/" data-counturl="http://www.r-bloggers.com/accessing-dataframe-objects-exercises/" data-count="vertical" data-text="Accessing Dataframe Objects Exercises" 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://r-exercises.com/2016/05/20/accessing-dataframe-objects-exercises/"> R-exercises</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/D1ErWPxCVuI"></a></div>