Basics of R and Rstudio

Author

Steen Flammild Harsted

Published

April 25, 2025

1 Presentation

You can download the course slides for this section here

   

2 Rstudio

Together in groups of two or three explain the following to each other:

  • What and where is the Console?
  • What and where is the Script?
  • What is the difference between writing 2+2in the console and the script?
  • What happens if you write a # in the script??
  • When and why can comments in your code be useful?
  • What and where is the Environment Pane?
  • What and where is the “Files, Help, Plots, Packages, and Viewer” Pane?
  • What does F1 and ?[some_function] do?


   

   

3 Functions

3.1 The assignment operator <-

  • Do you have a short cut for the assignment operator? (Tools -> Show Command Palette -> and type “assignment”)
  • What does the assign operator <- do?


Assign the value 42 to an object called answer_to_everything

Show me the code
answer_to_everything <- 42


3.2 c()

The c is short for concatenate, and means to link together.
This function combines values into a vector. For now you can think of a vector as a sequence of values. The values are seperated by a ,


Use c() to create a sequence of numbers from 0 to 4

Show me the code
c(0, 1, 2, 3, 4)

 

Assign the sequence to an object called my_sequence

Remember <- ::: {.cell}

Show me the code
my_sequence <- c(0, 1, 2, 3, 4)

:::

 

3.3 sample()

Use the function sample() to take a sample of 3 random values from my_sequence

Show me the code
sample(my_sequence, 3)

What does the argument replace do? What is the default value?

  

Use the function sample() to take a sample of 10 random values from my_sequence

Show me the code
sample(my_sequence, 10, replace = TRUE)

3.4 mean()

Another function we can use is mean(). This function gives you the mean value of a sequence of numbers. Read the arguments section of the help page for mean()


What type of input does the first arguments of the mean() function require?

Show me the code
# It requires a vector. 
# You can still think of this as a sequence of numbers
# c()


Take the mean of my_sequence

Show me the code
mean(my_sequence)


Take the mean of this vector c(2, 4, 6, NA)

Show me the code
mean(c(2, 4, 6, NA))


Take the mean of this vector c(2, 4, 6, NA), disregarding NA values

Read about the na.rm argument in the mean() function.
Use F1 or ?mean
What is the default value?

Show me the code
mean(c(2, 4, 6, NA),
     na.rm = TRUE)


You will find the na.rm argument in many functions. It always defaults to FALSE.

  • Discuss if this is a good idea?
  • Is it different from other programs?

 

3.5 The pipe |>

  • What is your keyboard shortcut to write the pipe |> ? (Pres CTRL+SHIFT+P and type pipe in the search field).


Using the pipe, take the mean of my_sequence.

Show me the code
my_sequence |> mean()


Using the pipe, take my_sequence and then sample 10 random numbers and then take the mean

Show me the code
my_sequence |>
  sample(10, replace = TRUE) |> 
  mean()


Rewrite this code to use the pipe instead

sort(
  sample(my_sequence,
         10,
         replace = TRUE)
  )
Show me the code
my_sequence |> 
  sample(10,
         replace = TRUE) |> 
  sort()

  

3.6 Explore

Investigate the following functions that we may need later on in this course.

  • quantile()
  • rnorm()
  • median()
  • cumsum()
  • min()
  • max()
  • n()
  • set.seed()