R Course

R programming

R is a language that may be helpful to do mathematics. We will use it in other course such as Statistics, Matrices, Numerical analysis or Optimization.

You can use other languages such as Python, Julia, OCaml, according to what you want to do.


Introduction

The file extension is .R but we also write .Rmd files that are files with documentation and runnable R code. You may also directly run some code in the console.

Most people code in R using R Studio, but I will use R plugin for IntelliJ that working exactly the same as R Studio (same interface elements, console, etc.) but in IntelliJ.

You may have to install packages or libraries in the future, simply do

  • install a library : install.packages("name")
  • load a library (runtime) : library("name");

Basic course

You can check this fast course to learn the basics of R without details Learn R in 5 minutes.

Then here is the real course.

And here you can learn how to write Rmd documentation.

Other notes about the Language
The semicolon ; at the end of a line is optional. You may use it if you make more than one expression per line.
Use # for comments
Use print(value) to print something.
x <- "Hello"
y <- "World"
print(paste(x, y))
# [1] "Hello World"
cat(paste(x, y), sep = "\n")
# Hello World

You will use this a lot, you can see the help for a function with ?my_function

  • examples with examples(my_function)
  • args with args(my_function)
  • source code with body(my_function)
Open the documentation either in your IDE or your browser with help.start().
Either look for or load a dataset with data()/data('dataset').
In R, when you are making a generalization of something, you usually name it name.generalization_name. For instance, if you got a vector of numbers data$numbers, then you would have data$numbers.pos. Same for functions like mean, mean.test, ...
There are a lot of shortened words or it seems so. For instance, you can use T for TRUE. For functions, you can use prob/proba for probability etc.

Advanced notes

Here you can find some notes about functions that you might use (mainly in statistics).


Sources

  • « Take only pictures, leave only footprints. »
  • Swirl, R programming
  • https://pbil.univ-lyon1.fr/R/pdf/lang01.pdf (01-04)
  • Initiation à R - Eric Preud’homme
  • Introduction à R - Christophe Lalanne & Bruno Falissard
  • R - Vincent ISOZ, Daname KOLANI
  • Régression avec R - Pierre-André Cornillonn & Eric Matzner-Løber

References