Basics: Running, Commenting, Arithmetic Operations

1.5 Running code

Code written in R scripts or a R Markdown code chunk does not get executed until you run it:

  • To run a line of code in an R script, put the cursor on that line and click “Run”. Keyboard shortcut: Windows: Ctrl + Enter, Mac: Cmd + Return
  • To run a selected region of code, highlight the region and click “Run”.
  • To run an entire R script, click “Source”.
  • To run a code chunk in R Markdown, click the green triangle on the top-right.

Commands will be executed in the console.

1.6 Commenting

A good practice for a programmer is to include comments that explain what a chunk of code performs. This helps other people understand your code, and it also serves as a note to yourself for later reference.

  • To write comments in a script/code chunk, put # in front of the line to be commented out.

Another use of comments is to “mute” a portion of code — say you have written 10 lines of code. You want to run everything but line 2, but you don’t want to delete line 2. Then, you can simply add a # in front of line 2. Whatever follows # in line 2 will not be treated as code.

  • To convert a region of the code to comment, highlight the area and click the magic wand -> “Comment/Uncomment Lines”. Keyboard shortcut: Windows: Ctrl+Shift+C, Mac: Cmd+Shift+C.

1.7 Arithmetic operations

Now we’ll start writing some actual R code. Here we’ll also illustrate running and commenting.

R can be used as a calculator. Below are some commonly used arithmetic operations:

Operation Example Example code
Addition \(1+2\) 1+2
Subtraction \(2-1\) 2-1
Multiplication \(3\times 2\) 3*2
Division \(4\div2\) 4/2
Exponent \(2^3\) 2^3
Square root \(\sqrt{9}\) sqrt(9)
Natural exponent \(\exp(2)=e^2\) exp(2)
Natural log \(\ln(5)\) log(5)

Let’s see a few examples.

  1. Simple operations:

The box in gray is a code chunk, and the gray box right below it (starting with ##) is the corresponding output. You can click the upper-right corner (there’s a copy icon) to copy the code to your R script/markdown.

  • \(3+5-2\):
3 + 5 - 2
## [1] 6
  • \(4\times (8+1)\):
4 * (8 + 1)  # be aware of the order of operations (note: this is a comment)
## [1] 36
  • \(5\div2\):
5 / 2
## [1] 2.5
  1. Powers and roots
  • To compute \(2^4\):
2 ** 4   
## [1] 16
#same as:
2^4
## [1] 16
  • To compute \(\sqrt{4}\), i.e. \(4^{1/2}\):
sqrt(4) 
## [1] 2
#same as:
4^(1 / 2)
## [1] 2
  1. Remainder
  • To compute the remainder of \(15\div 6\):
15 %% 6
## [1] 3
  • To compute the remainder of \(15\div 2\):
15 %% 2
## [1] 1

The remainder of something divided by 2 can help R determine whether a number is even or odd. A even number has remainder 0, and an odd one has remainder 1.

  1. Exponents and logarithms
  • To get \(e\) (i.e., \(e^1\)):
exp(1)
## [1] 2.718282
  • To get the \(\ln(1) = \log_e(1)\):
log(1)
## [1] 0
  • Recall that \(\ln(\exp(x)) = x\):
log(exp(1))
## [1] 1
log(exp(2))
## [1] 2

log() and exp() are typical functions in R, where log, exp are the function names, and the inputs (called the arguments) go inside the ().

We’ll learn more about functions later. For now, note that a function can take multiple inputs. An example is log, try running ?log to pull out its documentation. There are two arguments, x (the number to take the \(\log\) of), and base (the base of the logarithm). Essentially, this function computs \(\log_{base}(x)\) based on the base and x values you provide.

So how come you could only specify one argument in log(1)? This is because some arguments have default values. In this case, x does not have default (so you have to specify x), but base has a default of exp(1), i.e., \(e\). This is why log(x) is equivalent to \(log_e(x).\) However, if you want to use a different base, you can specify a second argument base, taking another value.

  • To compute \(\log_2(4):\)
log(4, base = 2) 
## [1] 2
#same as:
log(4, 2)     
## [1] 2
  1. Special values and rounding
  • \(\pi\):
pi
## [1] 3.141593
  • Rounding up/down:
ceiling(pi)
## [1] 4
floor(pi)
## [1] 3
  • Round to 2 decimal points:
round(pi, 2)
## [1] 3.14

1.7.1 Order of operations

When you have more than one operation performed, e.g., \(5-(8-4)\), you need to consider which operation is performed first.

The rules for order of operations in R are highly similar to those in math:

  • Doing things in the parenthesis first: For example, if you run 5*(8-(2-1)), the operation in the inner parenthesis, (2-1)=1, will be done first. Then, R does (8-1) =7. Lastly, it does 5*7=35.
5 * (8 - (2 - 1))
## [1] 35
  • Exponents (powers, roots, \(\exp\), \(\log\)) first, then multiplication/division, lastly add/subtract: For example, if you run 5+8*2^2 (i.e., \(5+8\times 2^2\)), R first does 2^2 = 4, then does 8*4 = 32, lastly does 5+32 = 37.
5 + 8 * 2^2
## [1] 37
  • Otherwise, go from left to right: For example, if you run 1*2*3, R first does 1*2, then does 2*3.

Tip: You’d often need to use parentheses to specify what operation is prioritized. Whenever you are unsure, just use ()s to be safe.

1.8 Your turn

Try to write the R code for the following operations.

  1. \(5\times (2\div(3-1))\)

  2. \(3^2 + 4^2\)

  3. \(e^{1.5\times (2-1)}\)

  4. \((\sqrt{2})^{1+3}\)