1 Introduction

First class, introduction to R and how to use it

1.1 General R usage

Set up your directory

Install and load tidyverse

1.2 Basics :

  • R comments start with #
  • Use text editor and beware of ”smart quotes”
  • To get help about anything use ?: ?seq will show you the manual to use the function seq (a function which generates sequences of numbers)
  • To assign a value to a variable use the assignment operator <-

1.3 Explore dataset:

We will use the mpg dataset which is load by default with tidyverse

To have more info about this dataset:

To see the first lines of a dataset:

To see the structure of the data:

You can see from the output that this is a data.frame: a rectangular structure with column and lines (?data.frame if you want to learn more data.frame). As you can see each column may store data of different types: here int ie numbers and chrs ie string of characters.

To get the list of column names:

To get some simple summary statistics of the dataset:

1.4 Vectors

to create a vector use c() ### Exemple: * Numeric Vector

To select the ith element of a vector a use the notation a[i]. You can also select multiple values from the same vector by using the notation a[c(i,j)].

As you may have noticed this means you use a second vector to store the indices of the elements you want so the notation a[c(2,4)] could be rewritten as:

1.5 Matrices and arrays

All columns must be the same type (numeric, character, etc.) and the same length. Arrays are similar than matrices but can have more than two dimensions

1.7 Data Frame

1.9 For those who finished all

Can you reproduce the following plots? You will need some of the libraries listed below.

PLOT 1:

PLOT 2:

PLOT 3:

PLOT 4: (hint you will need to use theme_economist_white() from ggthemes)