R#1: Introduction to R and RStudio

Laurent Modolo laurent.modolo@ens-lyon.fr

10 Oct 2019

R#1: Introduction to R and RStudio

The goal of this practical is to familiarize yourself with R and the RStudio environment.

The objectives of this session will be to:

http://perso.ens-lyon.fr/laurent.modolo/R/session_1_a

Press [alt] + [shift] + k

Functions are also variables

We can also define our own function with

<FUNCTION_NAME> <- function(a, b){
  <RESULT_1> <- <OPERATION_1>(a, b)
  <RESULT_2> <- <OPERATION_2>(<RESULT_1>, b)
  return(<RESULT_2>)
}

write a function to test the base of the logarithm function

base_test <- function(x, base){
  log_result <- logarithm(x, base=base)
  exp_result <- base^log_result
  test_result <- x == exp_result
  return(test_result)
}

http://perso.ens-lyon.fr/laurent.modolo/R/1_b

Functions are also variables

base_test <- function(x, base){
  print(x)
  log_result <- logarithm(x, base=base)
  print(log_result)
  exp_result <- base^log_result
  print(exp_result)
  print(x)
  test_result <- x == exp_result
  return(test_result)
}

http://perso.ens-lyon.fr/laurent.modolo/R/1_c

Functions are also variables

base_test <- function(x, base){
  print(x)
  log_result <- logarithm(x, base=base)
  print(log_result)
  exp_result <- base^log_result
  print(exp_result)
  print(x)
  test_result <- isTRUE(all.equal(x, exp_result))
  return(test_result)
}

http://perso.ens-lyon.fr/laurent.modolo/R/1_d

Functions are also variables

base_test <- function(x, base){
  return(isTRUE(all.equal(x, base^logarithm(x, base=base))))
}

http://perso.ens-lyon.fr/laurent.modolo/R/1_e

The environment

A code editor

A code editor

RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can

If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made.

A code editor

Copy your logarithm and base_test into a tp_1.R file

We can now clean your environment

rm(x)
?rm
ls()
rm(list = ls())

Installing packages

install.packages("tidyverse")
install.packages("ggplot2")

Installing packages

Loading packages

sessionInfo()
library(tidyverse)
sessionInfo()
unloadNamespace("tidyverse")
sessionInfo()

Complex variable type

Vector (aka list)

c(1, 2, 3, 4, 5)
1:5
2^(1:5)
x <- 1:5
2^x
log(x)
logarithm(x)
base_test(x, base = 10)

Vector (aka list)

typeof(x)
typeof(x + 0.5)
is.vector(x)
length(x)
x[5]
y <- c(a = 1, b = 2, c = 3, d = 4, e = 5)
typeof(y)
is.vector(y)
names(y)
y[1]
y["a"]
names(y) <- c("b")
x == y
all.equal(x, y)

Vector challenge

Vector challenge

seq(from=2, to=10, by=2)

Vector challenge

seq(from=2, to=10, by=2)

Vector challenge

c(1:5, "a", "b", "c")
typeof(c(1:5, "a", "b", "c"))

Vector challenge

c(1:5, letters[1:3])

Vector challenge

rosette <- LETTERS
names(rosette) <- letters
rosette["b"]
rosette[13]

Matrix

In R matrix are two dimensional vectors

matrix_example <- matrix(1:(6*3), ncol=6, nrow=3)
matrix_example
class(matrix_example)
nrow(matrix_example)
ncol(matrix_example)
matrix_example[2, 3]

DataFrame

In R data.frame are table type with mixed type

data_frame_example <- data.frame(numbers=1:26, letters=letters, LETTERS=LETTERS)
data_frame_example
class(data_frame_example)
nrow(data_frame_example)
ncol(data_frame_example)
names(data_frame_example)
data_frame_example[2, 3]
data_frame_example["numbers"]

List

In R list are multitypes object

list(a = "test", b = 1)
list(a = "test", b = 1:5)
z <- list(
  a = "test",
  b = 1:5,
  c = data.frame(numbers=1:26, letters=letters, LETTERS=LETTERS)
)
list(
  a = "test",
  b = 1:5,
  c = z
)