Book Image

Mastering Scientific Computing with R

Book Image

Mastering Scientific Computing with R

Overview of this book

Table of Contents (17 chapters)
Mastering Scientific Computing with R
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Basic sample simulations in R


We already showed you the basics of generating random variables from common probability distributions in Chapter 2, Statistical Methods with R. For example, to simulate four numbers from a normal distribution with a mean of 10 and standard deviation of 3, we use the rnorm() function with the mean and sd arguments set to 10 and 3, respectively:

> rnorm(4, mean=10, sd=3)
[1]  9.546714  8.600795 14.344557 11.669767

Similarly, we can simulate 10 numbers from a Poisson distribution with a lambda of 3 with rpois(10, lambda=3). A table showing the function names for the most common distributions is available in Chapter 2, Statistical Methods with R.

We can also select random variables from a predetermined vector using the sample() function. This function allows you to randomly select a specified number of elements with the size argument from a vector, with or without replacing the values taken with the replace argument set to TRUE or FALSE, respectively. For example...