Generating random numbers
You've seen sample()
and it's dplyr
cousin, slice_sample()
for generating pseudo-random numbers from a set of values. A related task is to generate random numbers that follow a statistical distribution, like the uniform distribution or the normal distribution.
Each random number generation function has a name beginning with "r". It's first argument is the number of numbers to generate, but other arguments are distribution-specific. Free hint: Try args(runif)
and args(rnorm)
to see what arguments you need to pass to those functions.
n_numbers
is available and set to 5000
; ggplot2
is loaded.
This exercise is part of the course
Sampling in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Generate random numbers from ...
randoms <- data.frame(
# a uniform distribution from -3 to 3
uniform = ___,
# a normal distribution with mean 5 and sd 2
normal = ___
)