In this document I will comment line by line on the 7 lines of code.

Line 1

rm(list = ls())        # empty workspace

Here we see two R commands: ls() and rm(). If you type help(ls) or help(rm) (and then “return”“) in the console window, you will see R’s help information about these two commands. A lot of the text will be hard to understand but you might still learn a few useful things from a quick look. The examples at the bottom, are often useful.

In this case, ls() generates a list of all the R objects which are currently in your work-space. Then rm( ... ) removes them. Notice that there are two ways to pass arguments to R functions: by position, and by name. Most R functions have a long list of arguments some of which are obligatory and some optional. Usually, the obligatory ones come first and you can just fill them in one by one in the right order. You can forget about the optional ones, R will take the “default” value, unless you want something different from the default.

This line simply empties the workspace so that there is no interference between things you do later in the script with things you did yesterday or last week. It also helps to make your script reproducible. I’ll get the same result as you.

Line 2

set.seed(1234)    # set random seed (for reproducibility)

This line is also all about reproducibility. I will get exactly the same random numbers as you do.

Line 3

Data <- rgamma(1000, shape = 5, rate = 1)

Data is now a vector of length 1000 containing a random sample from the gamma distribution with shape parameter 5 and rate parameter 1. Do help(rgamma) to find out about this function.

Line 4

library(MASS)                          # library containing some useful extra functions

This line makes available some useful extra functions contained in a library called MASS.

Line 5

truehist(Data, xlim = c(0, 20))   # "truehist" in MASS, is better than the standard "hist"

This line draws a nice histogram. The vertical axis is scaled so that the total area equals to 1. Scaled this way, the histogram is an estimator of the probabilty density.

The argment xlim says that the graph should have the x-axis running from 0 to 20.

Line 6

xpts <- seq(from = 0, to = 25, length = 1000)

xpts is a sequence of equally spaced numbers.

Line 7

truehist(Data, xlim = c(0, 20)) # redraw the histogram
lines(xpts, dgamma(xpts, shape = 5, rate = 1))

The function lines() adds a line jointing the points with x and y coordinates given by the first two arguments. The function dgamma() computes a gamma probabilty density. When you give it a vector as first argument, the result is a vector of the same length.

I had to redraw the histogram because I had already let it be seen.