<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-structures-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/03/15/data-structures-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=data-structures-exercises"> 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/2017/01/database-151392_960_720.png?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4716" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2017/01/database-151392_960_720-150x150.png?resize=150%2C150" data-recalc-dims="1"></a>There are 5 important basic data structures in R: vector, matrix, array, list and dataframe. They can be 1-dimensional (vector and list), 2-dimensional (matrix and data frame) or multidimensional (array). They also differ according to homogeneity of elements they can contain: while all elements contained in vector, matrix and array must be of the same type, list and data frame can contain multiple types.</p>
<p>In this set of exercises we shall practice casting between different types of these data structures, together with some basic operations on them. You can find more about data structures on <a target="_blank" rel="nofollow" href="http://adv-r.had.co.nz/Data-structures.html">Advanced R – Data structures</a> page.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/03/15/data-structures-solutions/">here</a>.</p>
<p>If you have different solution, feel free to post it.</p>
<p><strong>Exercise 1</strong></p>
<p>Create a vector named <code>v</code> which contains 10 random integer values between -100 and +100.</p>
<p><strong>Exercise 2</strong></p>
<p>Create a two-dimensional 5×5 array named <code>a</code> comprised of sequence of even integers greater than 25.</p>
<p>Create a list named <code>s</code> containing sequence of 20 capital letters, starting with ‘C’.</p>
<p><strong>Exercise 3</strong></p>
<p>Create a list named <code>l</code> and put all previously created objects in it. Name them a, b and c respectively. How many elements are there in the list? Show the structure of the list. Count all elements recursively.</p>
<p><strong>Exercise 4</strong></p>
<p>Without running commands in R, answer the following questions:</p>
<ol>
<li>what is the result of <code>l[[3]]</code>?</li>
<li>How would you access random-th letter in the list element <code>c</code>?</li>
<li>If you convert list <code>l</code> to a vector, what will be the type of it’s elements?</li>
<li>Can this list be converted to an array? What will be the data type of elements in array?</li>
</ol>
<p>Check the results with R.</p>
<p><strong>Exercise 5</strong></p>
<p>Remove letters from the list <code>l</code>. Convert the list <code>l</code> to a vector and check its class. Compare it with the result from exercise 4, question #3.</p>
<p><strong>Exercise 6</strong></p>
<p>Find the difference between elements in <code>l[[&quot;a&quot;]]</code> and <code>l[[&quot;b&quot;]]</code>. Find the intersection between them. Is there number 33 in their union?</p>
<p><strong>Exercise 7</strong></p>
<p>Create 5×5 matrix named <code>m</code> and fill it with random numeric values rounded to two decimal places, ranging from 1.00 to 100.00.</p>
<p><strong>Exercise 8</strong></p>
<p>Answer the following question without running R command, then check the result.</p>
<p>What will be the class of data structure if you convert matrix <code>m</code> to:</p>
<ul>
<li>vector</li>
<li>list</li>
<li>data frame</li>
<li>array?</li>
</ul>
<p><strong>Exercise 9</strong></p>
<p>Transpose array <code>l$b</code> and then convert it to matrix.</p>
<p><strong>Exercise 10</strong></p>
<p>Get union of matrix <code>m</code> and all elements in list <code>l</code> and sort it ascending.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Matrix exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2015/11/28/matrix-exercises/">Matrix exercises </a></li>
<li><a title="Array exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2015/12/01/array-exercises/">Array exercises </a></li>
<li><a title="Mode exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/02/14/mode-exercises/">Mode exercises </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/03/15/data-structures-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=data-structures-exercises"> 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/uC0JOqPQP8o"></a></div>