Basics of R code

Author

Steen Flammild Harsted

Published

January 10, 2024

   

1 Functions

1.1 <-

  • 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 inbuilt datset iris to an object called my_data

iris is available to you when you load R. It´s an inbuilt dataset. So it is available to you even though you cant find it in the Environment pane. Just type iris.

Code
my_data <- iris


1.2 c()

The c is short for concatenate, and means to link together.
This function combines values into a vector or list. 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

Code
c(0, 1, 2, 3, 4)

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

Code
c(0, 2, 4)

 

1.3 seq()

Of course, such predictable operations can be done with ease in a more reliable way. We will use the function seq() as an example. The output of this function is a sequence of numbers. The sequence of numbers is dictated by the arguments that you provide.

Read the arguments section on help page for seq()
Type ?seq() in the console or type seq()and pres F1 while the cursor stands on the on the letters.


Make a sequence of numbers that goes from 0 to 100

Code
seq(from = 0,
    to = 100)

# This simple operation can also be done by just writing 0:100


Make a sequence of numbers that goes from 0 to 100 in steps of 2

Code
seq(from = 0,
    to = 100,
    by = 2)

 

1.4 sample()

Using sample(), take a sample of 5 random numbers between 1 and 100

Code
sample(1:100, 5)

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

  

1.5 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?

Code
# It requires a vector. 
# You can still think of this as a sequence of numbers
# c()


Using c(), take the mean of the numbers 5, 3, 1, and 10

Code
mean(c(5, 3, 1, 10))


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

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. What is the default value?

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?

 

1.6 %>%

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

Load the tidyverse

Why? The %<% is a part of the tidyverse, and is not included in Base R

library(tidyverse)


Using the pipe, create a sequence of numbers going from 0 til 100 and then take the mean of the sequence.

Code
seq(from = 0,
    to = 100) %>%
  mean()


Using the pipe, create a sequence of numbers going from 1 to 20 in 39 steps, and then sample 10 random numbers from the sequence

Code
seq(from = 1,
    to = 20,
    length.out = 39) %>% 
  sample(10)


Rewrite this code to use the pipe instead

sort(
  sample(seq(1:100),
         10,
         replace = TRUE)
  )
Code
seq(1:100) %>% 
  sample(10,
         replace = TRUE) %>% 
  sort()

  

1.7 Explore

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

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