1 of 19

Class 2

Introduction to R

GEOG 215

2 of 19

AGENDA

Today’s Class …

  • What is R
  • Why R?
  • R Studio Interface
  • R Markdown
  • Working Directories and File Paths
  • R Packages

During Class …

  • In class demonstration
  • File paths exercise

Next Class …

  • In Class Activity

3 of 19

What is R?

  • Statistical programming language
    • Analyses
    • Graphics
    • Maps
    • Computation
  • Developed in the 1990s
  • Object-oriented
    • Organizes design around data, or objects, not function or logic (procedural or functional programming)

Class 2

3

4 of 19

Pros and Cons

Class 2

4

Very powerful - all steps of data science process

Versatile - spatial and non spatial data

Open-source and popular

Spread across many packages

Base R functionality is not sufficient

Much slower compared to other programming languages

Uses a lot of memory

Not as good for “Big Data”

5 of 19

R and R Studio

Class 2

5

R – refers to the programming language and the software that interprets the code

RStudio – Graphical user interface and wrapper (GUI) for R. Extends functionalities of R and helps with writing code

6 of 19

R Markdown (.Rmd)

Class 2

6

7 of 19

Storage Options

  • R File (.R)**
  • R Markdown (.Rmd)**
    • R code inside chunks
  • R project (.Rproj)
  • R environment (.Rdata)
    • Not recommended

Class 2

7

Be very careful that you are not relying on data already in the R environment from previous sessions!

Always read data in for each separate .R file or .Rmd file!

8 of 19

Source

9 of 19

Console

10 of 19

Environment/History

11 of 19

Files and more

12 of 19

Working Directory

  • Default location where R will look for files
    • Loadable and savable files

How does R / RStudio know what is the working directory?

  • If …. opening RStudio directly... it is your “home” folder
  • If …. opening RStudio from a .R or .Rmd file location of the file, you opened
  • If… knitting an .Rmd document, where the .Rmd is stored!

Class 2

12

13 of 19

Working Directory Cntd.

  • Getwd()
  • Setwd()

Class 2

13

14 of 19

File Paths

Class 2

14

Absolute

    • Points to a specific location/file on your computer
    • /home/Cardwell/GEOG215/assignments
      • Assignment_1
      • Assignment_2

Relative

    • Points to a location “relative” to current working directory (for example work directory is assignment 2 but you want to access assignment 1 data)
    • ../assignment_1/data

15 of 19

  • Best practice is to ALWAYS use relative file paths. Using absolute file paths is often problematic and is the #1 thing that causes issues in this course.
  • Visit the File Path help document on Canvas

Class 2

15

16 of 19

Console vs Script

  • Can type commands directly in console
    • However, all console commands are “forgotten” after each session
  • Script is key for reproducibility

Class 2

16

17 of 19

Packages

  • Created by other users
  • Libraries of functions
  • Provided to community at no charge
  • Extends the capacity of R and allows us to do much more
  • Often improve on Base R functions
  • CRAN is the main repository for packages

Class 2

17

18 of 19

Packages

Class 2

18

19 of 19

Introduction to R In Class Exercise