Doing mathematics with R: Introduction
Style guide
There is no singlecorrect way to write your R program. But you can certainly get a lot wrong (or shall we say, unreadable). We don't require you to adhere to any style, but we strongly recommend that you adhere to the conventions below.
Commentary
Comments are a way to make your code readable for others. But that other person is also you, if you see the code again after hours, days, weeks or months.
Too many comment is wrong. Too few comment sis also wrong. But where is the middle ground? A rule of thumb is to break the code into small blocks, each containing a few lines of code, and comment each block with a line.
One of the following two questions is usually very relevant to answer:
- What does this block of code do?
- Why was it implemented this way?
This is how you explain exactly what you store in the variable:
# compute the mean
mean_value <- sum_of_numbers / number_of_tests
Within programming code you do not write full sentences, but to keep things readable you put a space after the #
symbol.
So not like this:
#compute the mean
# Compute the mean.
Avoid lines longer than 79 characters; this way you can be sure that it can be displayed properly on a reasonably sized screen.
Indentation
Indentation is adding white space to the beginning of a line to make structure visible. This is not only for readability in R: in many cases it is mandatory in R to add white space. This must be done consistently for the readability and correct operation of the program.
computed_sum <- function(x, y) { result <- x + y return(result)
}
As here, use at least 2 spaces to keep the distinction clear. RStudio has an R customised editor for creating RScripts; use the preset indentation!
In R, you can generally assume that after each line ending in a brace ({
), the next line must be written with an extra level of indentation. This applies to all subsequent lines that are subordinate to the line ending with a brace ({
).
# the function below consists of four instructions
computed_sum <- function(vector_of_numbers) {
result <- 0
# the for-loop below consists of one line that is repeated
for (i in vector_of_numbers) {
result <- result + i
}
return(result)
}
Naming
Many elements in your R program can get a name, including functions and variables.
Function names can be as long as you like, but they cannot contain spaces or forbidden characters (such as a question mark or an operator such as a colon), or begin with a number. When composing a name from subwords, it is wise to separate these subwords with underscores. Variable names are usually shorter than function names, but one or two subwords improves readability. There are few reasons to abbreviate names; in this case use underscores to separate participles from each other.
Another less flexible, but still widely used convention, which is not based on the underscore is to start each successive participle with a capital letter (e.g, rectificySignal
as function name). We will use this sparingly in the course and preferably only for functions. R uses in its basic setting mostly periods to separate subwords. Use of the underscore is then a simple mechanism to avoid name conflicts and distinguish our own invented names from R's internal names. Besides, most modern programming language have adopted the underscore as separator of subwords in names
Obviously, you can adopt common mathematical symbolism when tackling a mathematical problem. Most other problem domains have no such symbolism and require longer names.
gcd <- function{} : # greatest common divisor
mean_of_number <- function(V) {} # the name says it all
dx <- 0.001 # a small increase
remaining_amount <- remaining_amount - 1000 # nothing to add
Additional white space
As with other media where texts are written to be read, we often add white space in code to increase readability. Indentation is an example of this, but it can be done in a number of ways.
If you divide code into small blocks, with a line of comments above it, for example, you add a blank line above that comment.
Operators are very compactly named functions, such as +
, ==
, or %
. Code that uses operators can be kept legible by inserting a space before and after the operator. You can use this as a rule of thumb, but also use your own common sense. We give some examples:
c <- a + b
x <- 2*x - 1
hypot2 <- sqrt(x^2 + y2)
= or <-
To assign a value to a variable, you can use both =
and <-
as assignment operators. There are minor differences (for example in priority rules), but we prefer <- . One reason is that an instruction like n = n + 1
to increment by 1 can be confusing from a mathematical point of view. Simultaneously pressing the Alt and - keys gives you the arrow key at once.