Skip to main content

2.11) Tackling Complicated Code


Before we move onto the next session, try this revision exercise.

Here is a piece of R code from one of the Examples at the end of the help file for plot(). It looks complicated:

plot(table(rpois(100,5)), type = "h", col = "red", lwd = 10,
main = "rpois(100, lambda = 5)")

because there are several functions nested inside each other, as well as several named arguments. To understand what it does, delve inside the brackets and break it down into manageable chunks. First, try the function with the innermost pair of brackets:

rpois(100,5)

Clearly this produces 100 numbers. To find out where they come from, get help:

?rpois

Then try the next brackets out:

table(rpois(100,5))

...and you can probably guess what this function does. Finally, check the Arguments part of the plot() help file to see what the other arguments do. We can find "type" and "main". We can’t find "col" or "lwd" – but we do see an ellipsis (...), which stands for “Arguments to be passed to methods, such as graphical parameters (see par).” And sure enough, we can follow the hyperlink to the par() help file, and there we’ll find the meanings of the other two arguments.

Look carefully at the code above. Why do you think h, red and rpois(100, lambda = 5) are in inverted commas?