<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/probability-functions-beginner/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/08/17/probability-functions-beginner/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/HS_part1-11.png" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/HS_part1-11-150x150.png?resize=150%2C150" alt="" class="wp-image-6719" data-recalc-dims="1"></a>On this set of exercises, we are going to explore some of the probability functions in R with practical applications. Basic probability knowledge is required.</p>
<p>Note: We are going to use random number functions and random process functions in R such as <code>runif</code>, a problem with these functions is that every time you run them you will obtain a different value. To make your results reproducible you can specify the value of the seed using <code>set.seed(‘any number’)</code> before calling a random function. (If you are not familiar with seeds, think of them as the tracking number of your random numbers). For this set of exercises we will use <code>set.seed(1)</code>, don’t forget to specify it before every random exercise.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/08/17/probabilty-functions-beginner-solution/" target="_blank">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></p>
<p><strong>Generating random numbers. </strong> Set your seed to 1 and generate 10 random numbers using <code>runif</code> and save it in an object called <code>random_numbers</code>.</p>
<p><strong>Exercise 2</strong></p>
<p>Using the function <code>ifelse</code> and the object <code>random_numbers</code> simulate coin tosses. Hint: If <code>random_numbers</code> is bigger than .5 then the result is head, otherwise is tail.</p>
<p>Another way of generating random coin tosses is by using the <code>rbinom</code> function. Set the seed again to 1 and simulate with this function 10 coin tosses. Note: The value you will obtain is the total number of heads of those 10 coin tosses.</p>
<p><strong>Exercise 3</strong></p>
<p>Using the function <code>rbinom</code> to generate 10 unfair coin tosses with probability success of 0.3. Set the seed to 1.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/08/17/probability-functions-beginner/"></aside>
<div><strong>Learn more</strong> about probability functions in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/statistics-with-r-advanced-level/" target="_blank">Statistics with R – Advanced Level</a>. In this course you will learn how to
<ul>
<li>work with different binomial and logistic regression techniques,</li>
<li>know how to compare regression models and choose the right fit,</li>
<li>and much more.</li>
</ul>
</div>
</div>
<p><strong>Exercise 4</strong></p>
<p>We can simulate rolling a die in R with <code>runif</code>. Save in an object called <code>die_roll</code> 1 random number with <code>min = 1</code> and <code>max = 6</code>. This mean that we will generate a random number between 1 and 6.</p>
<p>Apply the function <code>ceiling</code> to <code>die_roll</code>. Don’t forget to set the seed to 1 before calling <code>runif</code>.</p>
<p><strong>Exercise 5</strong></p>
<p>Simulate normal distribution values. Imagine a population in which the average height is 1.70 m with an standard deviation of 0.1, using <code>rnorm</code> simulate the height of 100 people and save it in an object called <code>heights</code>.</p>
<p>To get an idea of the values of heights applying the function <code>summary</code>to it.</p>
<p><strong>Exercise 6</strong></p>
<p>a) What’s the probability that a person will be smaller than 1.90? Use <code>pnorm</code><br>
b) What’s the probability that a person will be taller than 1.60? Use <code>pnorm</code></p>
<p><strong>Exercise 7</strong></p>
<p>The waiting time (in minutes) at a doctor’s clinic follows an exponential distribution with a rate parameter of 1/50. Use the function <code>rexp</code> to simulate the waiting time of 30 people at the doctor’s office.</p>
<p><strong>Exercise 8</strong></p>
<p>What’s the probability that a person will wait less than 10 minutes? Use <code>pexp</code></p>
<p><strong>Exercise 9</strong></p>
<p>What’s the waiting time average?</p>
<p><strong>Exercise 10</strong></p>
<p>Let’s assume that patients with a waiting time bigger than 60 minutes leave. Out of 30 patients that arrive to the clinic how many are expected to leave? Use <code>qexp</code></p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/25/hacking-statistics-exercises-part-4/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/13/hacking-statistics-exercises-part-6/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-6)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-6) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/03/23/lets-begin-with-something-sample/" title="Lets Begin with something sample" target="_blank">Lets Begin with something sample </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">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://www.r-exercises.com/2017/08/17/probability-functions-beginner/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/yQ8sHpm_tlI" height="1"></a></div>