Skip to content
Snippets Groups Projects
slides.Rmd 8.18 KiB
title: "R#1: Introduction to R and RStudio"
author: Laurent Modolo [laurent.modolo@ens-lyon.fr](mailto:laurent.modolo@ens-lyon.fr)
date: 10 Oct 2019
output:
  beamer_presentation:
    theme: "metropolis"
    slide_level: 3
    fig_caption: false
    df_print: tibble
    highlight: tango
    latex_engine: xelatex

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:

  • Understand the purpose of each pane in RStudio
  • Do basic computation with R
  • Define variables and assign data to variables
  • Manage a workspace in R
  • Call functions
  • Manage packages

Acknowledgments

\begin{columns} \begin{column}{0.5\textwidth} \includegraphics[width=\textwidth]{img/software_carpentry_logo} {\bf https://software-carpentry.org/} http://swcarpentry.github.io/r-novice-gapminder/ \end{column} \begin{column}{0.5\textwidth} \includegraphics[width=\textwidth]{img/r_for_data_science.png} \end{column} \end{columns}

Some R background

\includegraphics[width=40pt]{img/Rlogo.png} is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing.

Some R background

\includegraphics[width=40pt]{img/Rlogo.png}

  • Created by Ross Ihaka and Robert Gentleman
  • initial version released in 1995
  • free and open-source implementation the S programming language
  • currently developed by the R Development Core Team.

Some R background

Reasons to use \includegraphics[width=40pt]{img/Rlogo.png}

Some R background

\includegraphics[width=\textwidth]{img/R_terminal.png}

RStudio, the R IDE

\begin{block}{IDR: Integrated development environment} application that provides {\bf comprehensive facilities} to computer programmers for software development \end{block}

  • free
  • open source

An interface

\includegraphics[width=\textwidth]{img/RStudio.png}

The same console as before

\includegraphics[width=\textwidth]{img/RStudio_console.png}

R as a calculator

  • Add: +
  • Divide: /
  • Multiply: *
  • Subtract: -
  • Exponents: ^ or **
  • Parentheses: (, )

R as a calculator

1 + 100
1 +

\pause

3 + 5 * 2
(3 + 5) * 2

\pause

(3 + (5 * (2 ^ 2))) # hard to read
3 + 5 * 2 ^ 2       # clear, if you remember the rules
3 + 5 * (2 ^ 2)     # if you forget some rules, this might help

\pause

2/10000

\pause

2e-4 is shorthand for 2 * 10^(-4)

5e3

Mathematical functions

log(1)  # natural logarithm

\pause

log10(10) # base-10 logarithm

\pause

exp(0.5)

\pause

Compute the factorial of 9

\pause

factorial(9)

Comparing things

equality (note two equal signs read as "is equal to")

1 == 1

\pause

inequality (read as "is not equal to")

1 != 2 

\pause

less than

1 < 2

\pause

less than or equal to

1 <= 1

\pause

greater than

1 > 0

Variables and assignment

<- is the assignment operator in R. (read as left member take right member value)

x <- 1/40
x

The environment

\includegraphics[width=\textwidth]{img/RStudio_environment.png}

Variables and assignment

log(x)
x <- 100
log(x)

\pause

x <- x + 1
y <- x * 2

\pause

z <- "x"
x + z

Variables and assignment

Variable names can contain letters, numbers, underscores and periods.

They cannot start with a number nor contain spaces at all.

Different people use different conventions for long variable names, these include

periods.between.words
underscores_between_words
camelCaseToSeparateWords

What you use is up to you, but be consistent.

\pause It is also possible to use the = operator for assignment but don’t do it !

Variables and assignment

Which of the following are valid R variable names?

min_height
max.height
_age
.mass
MaxLength
min-length
2widths
celsius2kelvin

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

Functions are also variables

logarithm <- log

\pause

A R function can have different arguments