<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-frame-exercises-vol-2/"></div></div><div><a data-url="https://www.r-bloggers.com/data-frame-exercises-vol-2/" data-counturl="https://www.r-bloggers.com/data-frame-exercises-vol-2/" data-count="horizontal" data-text="Data frame exercises Vol. 2" 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/11/28/data-frame-exercises-vol-2/"> 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="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/01/table2.png?resize=133%2C127"><img alt="table2" class="wp-image-216" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/01/table2.png?resize=133%2C127" data-recalc-dims="1"></a></p>
<p>[In the exercises below we cover the basics of data frames.]</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/11/28/data-frame-exercises-vol-2-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<p>Consider two vectors:<br>
<code>x=seq(1,43,along.with=Id)</code><br>
<code>y=seq(-20,0,along.with=Id)</code><br>
Create a data.frame df:<br>
<code><br>
>df<br>
  Id Letter         x          y<br>
1   1      a  1.000000 -20.000000<br>
2   1      b  4.818182 -18.181818<br>
3   1      c  8.636364 -16.363636<br>
4   2      a 12.454545 -14.545455<br>
5   2      b 16.272727 -12.727273<br>
6   2      c 20.090909 -10.909091<br>
7   3      a 23.909091  -9.090909<br>
8   3      b 27.727273  -7.272727<br>
9   3      c 31.545455  -5.454545<br>
10  4      a 35.363636  -3.636364<br>
11  4      b 39.181818  -1.818182<br>
12  4      c 43.000000   0.000000<br>
</code></p>
<p><strong>Exercise 2</strong></p>
<p>From the previous one data frame df.<br>
Create this data frame:<br>
<code><br>
   Id      x.a        y.a       x.b        y.b       x.c        y.c<br>
1   1  1.00000 -20.000000  4.818182 -18.181818  8.636364 -16.363636<br>
4   2 12.45455 -14.545455 16.272727 -12.727273 20.090909 -10.909091<br>
7   3 23.90909  -9.090909 27.727273  -7.272727 31.545455  -5.454545<br>
10  4 35.36364  -3.636364 39.181818  -1.818182 43.000000   0.000000<br>
</code></p>
<p><strong>Exercise 3</strong></p>
<p>Create two data frame df1 and df2:<br>
<code><br>
> df1<br>
  Id Age<br>
1  1  14<br>
2  2  12<br>
3  3  15<br>
4  4  10<br>
> df2<br>
  Id Sex Code<br>
1  1   F    a<br>
2  2   M    b<br>
3  3   M    c<br>
4  4   F    d<br>
</code><br>
From df1 and df2 create M:<br>
<code><br>
>M<br>
 Id Age Sex Code<br>
1  1  14   F    a<br>
2  2  12   M    b<br>
3  3  15   M    c<br>
4  4  10   F    d<br>
</code></p>
<p><strong>Exercise 4</strong></p>
<p>Create a data frame df3:<br>
<code><br>
> df3<br>
  id2 score<br>
1   4   100<br>
2   3    98<br>
3   2    94<br>
4   1    99<br>
</code><br>
From M and df3 create N:<br>
<code><br>
Id Age Sex Code score<br>
1  1  14   F    a    99<br>
2  2  12   M    b    94<br>
3  3  15   M    c    98<br>
4  4  10   F    d   100<br>
</code></p>
<p><strong>Exercise 5</strong></p>
<p>Consider the previous one data frame N:<br>
1)Remove the variables Sex and Code<br>
2)From N, create a data frame:</p>
<p><code><br>
values   ind
<p>1 1 Id<br>
2 2 Id<br>
3 3 Id<br>
4 4 Id<br>
5 14 Age<br>
6 12 Age<br>
7 15 Age<br>
8 10 Age<br>
9 99 score<br>
10 94 score<br>
11 98 score<br>
12 100 score<br>
</p>
<p><strong>Exercise 6</strong></p>
<p>For this exercise, we’ll use the (built-in) dataset trees.<br>
a) Make sure the object is a data frame, if not change it to a data frame.<br>
b) Create a new data frame A:<br>
<code><br>
>A<br>
	      Girth Height    Volume<br>
mean_tree  13.24839     76  30.17097<br>
min_tree    8.30000     63  10.20000<br>
max_tree   20.60000     87  77.00000<br>
sum_tree  410.70000   2356 935.30000<br>
</code></p>
<p><strong>Exercise 7</strong></p>
<p>Consider the data frame A:<br>
1)Order the entire data frame by the first column.<br>
2)Rename the row names as follows: mean, min, max, tree</p>
<p><strong>Exercise 8</strong></p>
<p>Create an empty data frame with column types:</p>
<p><code><br>
> df<br>
 Ints       Logicals   Doubles    Characters<br>
 (or 0-length row.names)<br>
</code></p>
<p><strong>Exercise 9</strong></p>
<p>Create a data frame XY<br>
<code><br>
X=c(1,2,3,1,4,5,2)<br>
Y=c(0,3,2,0,5,9,3)<br>
> XY<br>
  X Y<br>
1 1 0<br>
2 2 3<br>
3 3 2<br>
4 1 0<br>
5 4 5<br>
6 5 9<br>
7 2 3<br>
</code><br>
1)looks at duplicated elements using a provided R function.<br>
2) keeps only the uniques lines on XY using a provided R function.</p>
<p><strong>Exercise 10</strong><br>
For this exercise, we’ll use the (built-in) dataset Titanic.<br>
a) Make sure the object is a data frame, if not change it to a data frame.<br>
b) Define a data frame with value 1st in Class variable, and value NO in Survived variable<br>
and variables Sex, Age and Freq.<br>
<code><br>
      Sex   Age Freq<br>
1    Male Child    0<br>
5  Female Child    0<br>
9    Male Adult  118<br>
13 Female Adult    4<br>
</code></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="https://www.r-bloggers.com/data-frame-exercises-vol-2/"></div></div><div><a data-url="https://www.r-bloggers.com/data-frame-exercises-vol-2/" data-counturl="https://www.r-bloggers.com/data-frame-exercises-vol-2/" data-count="vertical" data-text="Data frame exercises Vol. 2" 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/11/28/data-frame-exercises-vol-2/"> 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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0WcatgCgDP8"></a></div></code>