<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/combinations-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/combinations-exercises/" data-counturl="https://www.r-bloggers.com/combinations-exercises/" data-count="horizontal" data-text="Combinations 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/08/12/combinations-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" href="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/08/combsmall.png?resize=240%2C94"><img alt="combsmall" class="wp-image-1128" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/08/combsmall.png?resize=240%2C94" data-recalc-dims="1"></a>When doing data analysis it happens often that we have a set of values and want to obtain various possible combinations of them. For example, taking 5 random samples from a dataset of 20. How many possible 5-sample sets are there and how to obtain all of them? R has a bunch of functions that help with tasks like these: <code>expand.grid</code>, <code>combn</code>, <code>outer</code> and <code>choose</code>.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/08/12/combinations-solutions/">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>
You first throw a coin with 2 possible outcomes: it either lands heads or tails. Then you throw a dice with 6 possible outcomes: 1,2,3,4,5 and 6.<br>
Generate all possible results of your action.</p>
<p><strong>Exercise 2</strong><br>
Generate a multiplication table for numbers ranging from 1 to 10.</p>
<p><strong>Exercise 3</strong><br>
You have a set of card values:<br>
<code> values &lt;- c(&quot;A&quot;, 2, 3, 4, 5, 6, 7, 8, 9, 10, &quot;J&quot;, &quot;Q&quot;, &quot;K&quot;) </code><br>
and a set of suits representing diamonds, clubs, spades and hearths:<br>
<code> suits &lt;- c(&quot;d&quot;, &quot;c&quot;, &quot;s&quot;, &quot;h&quot;) </code><br>
Generate a deck of playing cards. (e.g. King of spades should be represented as Ks).</p>
<p>Note: function <code>paste(..., sep=&quot;&quot;)</code> can be used to combine characters.</p>
<p><strong>Exercise 4</strong><br>
Think about a game of poker using a standard deck of cards like the one generated earlier. Starting hand in poker consists of 5 cards and their order does not matter. How many different starting hands are there in total?</p>
<p><strong>Exercise 5</strong><br>
You have a set of colors to choose from:<br>
<code> colors &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;white&quot;, &quot;black&quot;, &quot;yellow&quot;) </code><br>
You have to pick 3 colors and you cant’ pick the same color more than once. List all possible combinations.</p>
<p><strong>Exercise 6</strong><br>
Using the same set of colors – pick 3 without picking the same more than once, just like in the previous exercise.<br>
List all possible combinations but this time sort each combination alphabetically.</p>
<p><strong>Exercise 7</strong><br>
You have the same choices of colors and have to pick 3 but this time you can pick the same color more than once.<br>
List all possible combinations.</p>
<p><strong>Exercise 8</strong><br>
You have the same set of colors but this time instead of having to pick 3 you can choose to pick either 1, 2 or 3.<br>
How many different choices can you make?</p>
<p><strong>Exercise 9</strong><br>
You have the same set of colors and you can choose to pick either 1, 2 or 3.<br>
Make a list of all possible choices.</p>
<p><strong>Exercise 10</strong><br>
There are 3 color palletes: the first one has 4 colors, the second has 6 colors and the third has 8 colors. You have to pick a pallete and then choose up to 5 (1, 2, 3, 4 or 5) colors from the chosen color pallete. How many different possibilities are there?</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/combinations-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/combinations-exercises/" data-counturl="https://www.r-bloggers.com/combinations-exercises/" data-count="vertical" data-text="Combinations 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/08/12/combinations-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/?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/EUI5Xd53cZI"></a></div>