Intro to R: Basic Functions

Author

Adam Roberts (adapted from Curt Signorino)

Published

August 29, 2025

Topics

  • R as a calculator
  • Assigning values to variables
  • Vectors
  • Basic vector operations

This file is a Quarto document that runs R code. It is divided into text and “code chunks,” where we can run R code.

As we work through topics, you can type in the commands in the console manually. You can also run individual lines of code in the document itself by either hitting Cmd-Return (on Mac) or Ctrl-Enter (Windows), or clicking on the Run button at the top of the editor pane. You can run entire code chunks by either clicking the green arrow in the top right corner of the code chunk, or hitting Ctrl-Shift-Enter. You should see the command executed in the R console pane.

Clearing Memory

Before starting a new activity, it’s best practice to clear memory so we can start with a fresh slate.

rm(list = ls()) # clear memory

What is a Comment?

Comments in R are preceded by the # symbol. They are ignored by the R interpreter. Execute the following lines, and notice how only one will be evaluated.

#2+2
3+3
[1] 6

R as a Calculator

We can use R’s console simply as a calculator. Try executing the following lines:

2 + 2
[1] 4
10 / 4
[1] 2.5
2^4
[1] 16

When doing arithmetic – or any calculations – be careful of the “precedence” of operations, i.e., which calculations are performed first. For example:

1 - 4 / 2 # evaluates as 1-(4/2) = -1
[1] -1
2^3 / 2   # evaluates as (2^3)/2 = 8/2 = 4
[1] 4

When in doubt, use parentheses () to control the evaluation order.

Numerous math and statistics functions are available in R:

sqrt(2)          # square root of 2
[1] 1.414214
log(7.1)         # R default: log(x) is ln(x)
[1] 1.960095
log(100, base = 10) # log base 10
[1] 2
log10(100)
[1] 2
exp(2)           # e^2
[1] 7.389056
factorial(5)     # 5! = 5*4*3*2*1
[1] 120

We can perform multiple calculations – but again, be careful of precedence:

log(sqrt(4))
[1] 0.6931472

Sometimes R will tell you when you’ve tried to do something you shouldn’t have:

log(0)    # -Inf = -infinity
[1] -Inf
log(-.01) # NaN = "not a number"
Warning in log(-0.01): NaNs produced
[1] NaN

Variables and Assignment

We can create variables (or objects) and assign values to them. When we use those variables in a calculation, they will evaluate to the value we assigned them.

For assignment, we can use <- or =.

Before you execute the following, look at your Environment tab. Notice that x, y, and z are not shown there. They haven’t been defined.

x <- 4
y = 2
z = 10 / 2

After executing the above three lines, notice that: 1. There was no output in the R console. 2. x, y, and z are now shown in the Environment pane.

x # let's see what they equal
[1] 4
y
[1] 2
z
[1] 5

Let’s see what w is equal to:

#w

We can perform calculations using variables we have already created:

(x - y) * z
[1] 10

We can assign those results to new variables:

frog = (x - y) * z
frog
[1] 10

Practice 1

See 02_Intro_to_R_pt1_Practice1.pdf.


Vectors

Vectors are one-dimensional containers for multiple scalar values – e.g., a list of numbers. (Note, though, that R uses the term “list” in a slightly different way.)

We can use the c() concatenate command to create a vector:

x = c(1, 2, 3, 4)
x
[1] 1 2 3 4

We can also create a sequence of values from a to b using a:b:

y = -2:2
y
[1] -2 -1  0  1  2

: is a special case of the more general seq() command, which has lots of options:

y2 = seq(-2, 2)
y2
[1] -2 -1  0  1  2
seq(-2, 2, by = 1)
[1] -2 -1  0  1  2
seq(-2, 2)
[1] -2 -1  0  1  2

when we analyze a variable in a dataset, we’ll think of it as a vector. more on that later.

Scalar operations on vectors:

y 
[1] -2 -1  0  1  2
y + 1 # adds 1 to each element 
[1] -1  0  1  2  3
y * 2 # multiplies each element by 2
[1] -4 -2  0  2  4

Vector Functions

Let’s combine x and y and call it z.

x 
[1] 1 2 3 4
y 
[1] -2 -1  0  1  2
z = c(x, y) 
z
[1]  1  2  3  4 -2 -1  0  1  2

Functions we can apply to vectors:

sum(z) 
[1] 10
prod(z) 
[1] 0
max(z) 
[1] 4
min(z)
[1] -2

Other useful functions

sort(z) 
[1] -2 -1  0  1  1  2  2  3  4
length(z)
[1] 9

Practice 2

See 02_Intro_to_R_pt1_Practice2.pdf

Tip: If you’re in the R console pane, you can use the up and down arrows to scroll through previous commands you’ve entered. If you want to edit one of those commands, scroll to it using the up arrow and then use the left arrow to move the cursor to where you want to start editing.