Practical 1

This practical introduces the basics of using RStudio Desktop1, including navigating its interface and using keyboard shortcuts.

TipHint

You may want to refer to the RStudio cheatsheet during this practical.

Opening RStudio

  1. Open RStudio on your computer2.

  2. Open a new script editor within RStudio.

  3. Practice using keyboard shortcuts:

    • Switch between the console and script editor with Ctrl + 1 and Ctrl + 2.

    • Scroll through the console history with and .

    • Search the console history with Ctrl + r.

    • Clear the console screen with Ctrl + l.

  4. (Optional) Pick a colour scheme in the settings.

  5. Review the RStudio cheatsheet. What is the keyboard shortcut to comment (and uncomment) the selected text?

Using R as a calculator

Type at the console to answer the following questions.

  1. What is \(5^7\)?
5^7
[1] 78125
# Or, another way of writing this:
5**7
[1] 78125
  1. What is the area of a circle with radius 5cm?
# We could have approximated pi, for example:

3.14 * 5^2
[1] 78.5
# However, R has a built-in constant 'pi' object (see ?pi) that we can use
# instead:

pi * 5^2
[1] 78.53982
  1. And the circumference?
2 * pi * 5
[1] 31.41593

Getting help

  1. Check the help for the fivenum function.

  2. What does the na.rm argument do?

  3. How can you calculate the modulo of two numbers?

TipHint

The modulo is the remainder after division of one number by another; see here for details. You may need to search online to answer this question.

Creating objects

  1. Define two new objects containing your first and last name as strings.
# Example for my name:

first_name <- "Ewan"
last_name <- "Carr"
  1. Use the paste command to concatenate these objects.
paste(first_name, last_name)
[1] "Ewan Carr"
# Note that we haven't stored the result. R will print the output to the
# console, but then forget the result. We need to assign the output to a
# new object to store it in memory:

full_name <- paste(first_name, last_name)
  1. Repeat the calculation of the circumference of a circle. This time:
    • Define a new variable radius that contains the value 5.
    • Use this variable in your calculation.
radius <- 5

2 * pi * radius
[1] 31.41593
TipReflection
  • What happens if you don’t assign something?
  • How can you tell what objects are currently assigned?
  1. Practice using str and typeof to check the structure and types of the objects you just created.
str(first_name)
 chr "Ewan"
typeof(first_name)
[1] "character"
str(radius)
 num 5
typeof(radius)
[1] "double"
  1. Remove an object from memory using rm or via the RStudio “Environment” pane.
rm(first_name)
rm(last_name)
rm(radius)

Working with vectors

  1. Create three vectors:
    • A sequence of numbers from 1 to 5;
    • The letters a, b, c, d, e;
    • Five random numbers.
TipHint

Use the functions c, seq, rnorm(5).

# A sequence of numbers
x <- c(1, 2, 3, 4, 5)
x <- seq(1, 5, 1)
x <- 1:5

# Letters a, b, c, d, e
y <- c("a", "b", "c", "d", "e")
y <- letters[1:5]

# Five random numbers
z <- rnorm(5)
  1. Create two vectors, a and b, each containing 10 random numbers (using rnorm).
a <- rnorm(10)
b <- rnorm(10)
  1. Add each value of a to the corresponding value of b, creating a new vector of length 10.
a + b
 [1]  1.25431757  0.11087943  0.09122368 -0.37900685  0.41393905  1.03466140
 [7] -1.69829496 -1.33491557 -1.36025278 -1.49510840
# To create a new vector, we must assign the result to a new object:
a_plus_b <- a + b
  1. Add 5 to each value of a.
a + 5
 [1] 5.910996 4.533201 4.424515 4.821496 5.196414 5.606193 4.797930 3.880984
 [9] 3.867809 4.949814
  1. Combine the two vectors into a single vector of length 20.
c(a, b)
 [1]  0.91099583 -0.46679884 -0.57548533 -0.17850443  0.19641358  0.60619320
 [7] -0.20206951 -1.11901580 -1.13219059 -0.05018584  0.34332174  0.57767827
[13]  0.66670902 -0.20050242  0.21752547  0.42846820 -1.49622546 -0.21589977
[19] -0.22806219 -1.44492256

Footnotes

  1. Referred to as ‘RStudio’ from herein.↩︎

  2. If you’re having problems installing R, you can use the online RStudio server provided by posit.↩︎