<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/creating-sample-datasets-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/creating-sample-datasets-exercises/" data-counturl="https://www.r-bloggers.com/creating-sample-datasets-exercises/" data-count="horizontal" data-text="Creating Sample Datasets – 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/10/07/creating-sample-datasets-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="http://r-exercises.com/wp-content/uploads/2016/10/ancient-coins.jpg?x61433"><img sizes="(max-width: 300px) 100vw, 300px" alt="ancient-coins" class="wp-image-2201" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/10/ancient-coins-300x202.jpg?resize=300%2C202" data-recalc-dims="1"></a><br>
Creating sample data is a common task performed in many different scenarios.</p>
<p> R has several base functions that make the sampling process quite easy and fast. </p>
<p> Below is an explanation of the main functions used in the current set of exercices: </p>
<p> 1. set.seed() – Although R executes a random mechanism of sample creation, set.seed() function allows us to reproduce the exact sample each time we execute a random-related function. </p>
<p> 2. sample() – Sampling function. The arguments of the function are:<br>
x – a vector of values,<br>
size – sample size<br>
replace – Either use a chosen value more than once or not<br>
prob – the probabilities of each value in the input vector. </p>
<p> 3. seq()/seq.Date() – Create a sequence of values/dates, ranging from a ‘start’ to an ‘end’ value. </p>
<p> 4. rep() – Repeat a value/vector n times. </p>
<p> 5. rev() – Revert the values within a vector. </p>
<p> You can get additional explanations for those functions by adding a ‘?’ prior to each function’s name. </p>
<p> Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/10/07/creating-sample-datasets-solutions/">here</a>.<br>
If you have different solutions, feel free to post them. </p>
<p><strong>Exercise 1</strong><br>
1. Set seed with value 1235<br>
2. Create a Bernoulli sample of 100 ‘fair coin’ flippings.<br>
Populate a variable called <code>fair_coin</code> with the sample results. </p>
<p><strong>Exercise 2</strong><br>
1. Set seed with value 2312<br>
2. Create a sample of 10 integers, based on a vector ranging from 8 thru 19.<br>
Allow the sample to have repeated values.<br>
Populate a variable called <code>hourselect1</code> with the sample results </p>
<p><strong>Exercise 3</strong><br>
1. Create a vector variable called <code>probs</code> with the following probabilities:<br>
‘0.05,0.08,0.16,0.17,0.18,0.14,0.08,0.06,0.03,0.03,0.01,0.01’<br>
2. Make sure the sum of the vector equals 1. </p>
<p><strong>Exercise 4</strong><br>
1. Set seed with value 1976<br>
2. Create a sample of 10 integers, based on a vector ranging from 8 thru 19.<br>
Allow the sample to have repeated values and use the probabilities defined in the previous question.<br>
Populate a variable called <code>hourselect2</code> with the sample results </p>
<p><strong>Exercise 5</strong><br>
Let’s prepare the variables for a biased coin:<br>
1. Populate a variable called <code>coin</code> with 5 zeros in a row and 5 ones in a row<br>
2. Populate a variable called <code>probs</code> having 5 times value ‘0.08’ in a row and 5 times value ‘0.12’ in a row.<br>
3. Make sure the sum of probabilities on <code>probs</code> variable equals 1. </p>
<p><strong>Exercise 6</strong><br>
1. Set seed with value 345124<br>
2. Create a biased sample of length 100, having as input the <code>coin</code> vector, and as probabilities <code>probs</code> vector of probabilities.<br>
Populate a variable called <code>biased_coin</code> with the sample results. </p>
<p><strong>Exercise 7</strong><br>
Compare the sum of values in <code>fair_coin</code> and <code>biased_coin</code> </p>
<p><strong>Exercise 8</strong><br>
1. Create a ‘Date’ variable called <code>startDate</code> with value 9th of February 2010 and a second ‘Date’ variable called <code>endDate</code> with value 9th of February 2005<br>
2. Create a descending sequence of dates having all 9th’s of the month between those two dates. Populate a variable called <code>seqDates</code> with the sequence of dates. </p>
<p><strong>Exercise 9</strong><br>
Revert the sequence of dates created in the previous question, so they are in ascending order and place them in a variable called <code>RevSeqDates</code> </p>
<p><strong>Exercise 10</strong><br>
1. Set seed with value 10<br>
2. Create a sample of 20 unique values from the RevSeqDates vector. </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/creating-sample-datasets-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/creating-sample-datasets-exercises/" data-counturl="https://www.r-bloggers.com/creating-sample-datasets-exercises/" data-count="vertical" data-text="Creating Sample Datasets – 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/10/07/creating-sample-datasets-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/w6XMcb8kBVI"></a></div>