Getting started with R

Data Processing and Visualization in R

Jeff Stevens (he/him)

2023-03-16

What is R?

R is a statistical programming language.

What is R?

R is a statistical programming language.

Source: Allison Horst

What is R?

R is a statistical programming language.

Source: Allison Horst

What is R?

R is a statistical programming language.

Source: Twitter

What is R?

R is a statistical programming language.

It allows you to flexibly

  • wrangle

  • analyze

  • visualize your data

  • create reproducible documents

Conventions

  • All code is in monospace font peach text

  • R functions end with parentheses: function()

  • Directory names end with slash: home/

  • Package names are surrounded by curly braces: {tidyverse}

  • Keyboard buttons separate keys with a plus: Ctrl+S

  • These are parentheses (), brackets [], and braces {}

  • Links are in light blue text

Getting started

Installing R

Download at https://r-project.org.

For Windows, also install Rtools

Getting started

Installing RStudio

RStudio is an Integrated Development Environment (IDE). Download from Posit

Packages

Base R is a core set of packages for all R installations.

Source: A ModernDive into R and the Tidyverse

Note

User-contributed packages can be found on the Comprehensive R Archive Network or CRAN.

Packages

Installing

In the console type

install.packages("<package_name>") where <package_name> is the name of the package.

Packages

Installing

Try installing the {palmerpenguins} package.

install.packages("palmerpenguins")

You can install multiple packages simultaneously by wrapping them with c(). For example,

install.packages(c("remotes", "here"))

Packages

Loading packages

Packages

Loading packages

Load the {here} package:

library("here")

Note

Every time you close your R session, you’ll have to reload the packages you were using.

Packages

Using

Let’s view the penguins data set from the {palmerpenguins} package.

Try this:

penguins

You should receive Error: object 'penguins' not found. Why did you receive this error?

Packages

But you can either load the {palmerpenguins} package, or use this trick to call a specific function from a specific package.

palmerpenguins::penguins

tidyverse

tidyverse

Core tidyverse packages

  • {ggplot2}, for data visualisation

  • {dplyr}, for data manipulation

  • {tidyr}, for data tidying

  • {readr}, for data import

  • {purrr}, for functional programming

  • {tibble}, for tibbles, a version of data frames

  • {stringr}, for strings

  • {forcats}, for factors

Homework

Before the first class period

Source: Allison Horst