an introduction to r - carleton university

39
An Introduction to R MacOdrum Library February 23, 2021

Upload: others

Post on 19-Oct-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to R - Carleton University

An Introduction to RMacOdrum Library

February 23, 2021

Page 2: An Introduction to R - Carleton University

Before We Begin

Go to:

http://madgic.library.carleton.ca/deposit/R

Workshop/PhD%20Africa/

And download the four files

Save those files in

C:/Users/YOU/Downloads

2

Page 3: An Introduction to R - Carleton University

Overview

▪ About R

▪ What can R be used for

▪ The First Steps

▪ Activities

▪ Bonus Data (Time Permitting)

3

Page 4: An Introduction to R - Carleton University

What is R?

▪ Language and Environment originally

designed for statistical computing and

graphics

▪ Scripting Language

▪ Provides a lot of flexibility

▪ Can be used for multiple tasks beyond

just statistical analysis

4

Page 5: An Introduction to R - Carleton University

Why is R good?

▪ Free

▪ Open Source

▪ Simple but powerful

▪ Lots of help online

▪ Can work with all different types of

data/documents

▪ Lots of different “packages” that are

used for specific analysis

▪ Replication is Easy

▪ So much that you can do with R5

Page 6: An Introduction to R - Carleton University

Where to get help?

▪ Google

▪ Youtube

▪ Stack Overflow

▪ R Cheat sheets

▪ Lots of eBooks found in your university

library

6

Page 7: An Introduction to R - Carleton University

What can I do with R?

▪ Statistical Analysis| Basic Stats

| Deep Learning, Machine Learning

| Text Analysis

| Geospatial Analysis

| Audio Analysis

▪ Cleaning Messy Data| Cleaning up spreadsheets, Data files

▪ Automation| Web Scraping

| Virtual Machine

| Roll through multiple files in one go

▪ Visualisations 7

Page 8: An Introduction to R - Carleton University

Where are we? Where are we?

8

Basic Stats

Page 9: An Introduction to R - Carleton University

Where are we? Where are we?

9

Text Analysis

Negative Tweets Positive Tweets

Page 10: An Introduction to R - Carleton University

Where are we? Where are we?

10

Deep Learning

Page 11: An Introduction to R - Carleton University

GeoSpatial and Spatial Analysis

11

Page 12: An Introduction to R - Carleton University

12

Automating

▪ Transform a Word Doc to Excel Spreadsheet

Page 13: An Introduction to R - Carleton University

13

Automating

Page 14: An Introduction to R - Carleton University

14

Web Crawling

I removed the video that was

here so that the slides could

be sent through email.

If you are interested in learning

more about web crawling, look

up the RSelenium package

Page 15: An Introduction to R - Carleton University

15

Scraping Data

Grab Data

from the Web

and Bring it

into R

Page 16: An Introduction to R - Carleton University

What You Can Do with R

16

Page 17: An Introduction to R - Carleton University

The First Steps

17

▪ Objects/Variables

▪ Data Types

▪ Functions

▪ Reading and Writing Data

▪ Operators

▪ Conditional Statements

▪ Loops

Page 18: An Introduction to R - Carleton University

This is R

18

Page 19: An Introduction to R - Carleton University

This is R + RStudio

19

Page 20: An Introduction to R - Carleton University

RStudio

20

▪ Open RStudio

▪ 4 Main Areas (Each Corner)| Script

| Console

| Environment/History

| Help/Plot

Page 21: An Introduction to R - Carleton University

Variables/Objects

21

▪ Object: Essentially a variable that can

store a value

Page 22: An Introduction to R - Carleton University

Data Types

22

▪ Vector| Collection of cells with a fixed size

▪ Matrix| Two-dimensional vector (row and column) “mathy”

▪ Array| A vector with one or more dimensions

• One dimensional array ~ A vector

• Two dimensional array ~ A matrix

▪ List| Can hold objects of different types

▪ Data Frame| A Table in which each column holds the same data type

Page 23: An Introduction to R - Carleton University

Functions

23

Function.Name(x=******, y = *******)

Function Arguments

Page 24: An Introduction to R - Carleton University

MyData <- read.csv(“datafile.csv” , Header = TRUE)

24

Function ArgumentsObject

(Data Frame)

MyData <- read.csv(“datafile.csv” , Header = TRUE)

Object

(Data Frame)

Page 25: An Introduction to R - Carleton University

Functions: Examples

25

print(“What we want to print”)

mean(A Numerical Object)

strsplit(x=A Character Vector, split=Where

we want to split)

Page 26: An Introduction to R - Carleton University

Help Function

For Help with functions:

26

▪ Within R| Type ?read.csv

| help(read.csv)

| Check the Help box in bottom-right of

RStudio

▪ Online| Google!

| StackOverflow

Page 27: An Introduction to R - Carleton University

Reading & Writing Data

▪ Different Functions:| read.csv()

| readLines()

▪ Many Different Packages for Different

Data| sas7bdat

| foreign

| officer

27

Page 28: An Introduction to R - Carleton University

Indexing

28

▪ There are a few ways to grab a specific

element or column/row of a data type

▪ We use square brackets [ ] to grab a

specific element

▪ Elements are indexed in numerical order

from 1 to n

Page 29: An Introduction to R - Carleton University

Indexing

29

We’ll make a vector called My.Vector

We can grab the first element by typing

My.Vector[1]

Or the fifth element by typing

My.Vector[5]

Page 30: An Introduction to R - Carleton University

Indexing

30

We’ll make a DataFrame called DataFrame1

In a two-dimensional object, we have to index

the row and column

Page 31: An Introduction to R - Carleton University

Indexing

31

To grab a specific element, we type

DataFrame1[Row #, Column #]

So to get “Calgary”, we type

DataFrame1[3,1]

Page 32: An Introduction to R - Carleton University

Indexing

32

To get a entire row:

To get the third row, type:

DataFrame1[3,]

(Just the row number, but make sure to add

the comma)

Page 33: An Introduction to R - Carleton University

Indexing

33

To get a entire column:

To get the third column, type:

DataFrame1[,3]

(Just the row number, but make sure to add

the comma)

Page 34: An Introduction to R - Carleton University

Indexing

34

OR

To get a entire row or column:

Dataframes and Lists, have named objects

like columns which can be grabbed by using $

Type

DataFrame$Column.Name

Type DataFrame1$Province will return:

Page 35: An Introduction to R - Carleton University

Conditional Statements

35

If Statement is TRUE than do this

We use curly brackets {} to open and close

our “if statement”

Page 36: An Introduction to R - Carleton University

Conditional Statements

36

if(Conditional Statement) {

thing to happen if Condition is TRUE

}

if(Conditional Statement){

thing to happen if Condition is TRUE

} else {

thing to happen if Condition is FALSE

}

Page 37: An Introduction to R - Carleton University

Loops

37

▪ Loops are used to repeat a specific task

over a block of code

▪ For Loops| Runs for a specific period of time (10 times, 20 times,

1000 times)

▪ While Loops| Runs until a specific condition is met (run until object is

greater than 10, run until you encounter a specific object)

Page 38: An Introduction to R - Carleton University

For Loops

38

Looks similar to our if statements

for (i in 1:10){

print(i)

}

i is an object

1:10 is a range of numbers

print(i) will be done for each value of i

Page 39: An Introduction to R - Carleton University

How to Get Help

39

▪ Within R| Type ?function

| Check the Help box in bottom-right of RStudio

▪ Online| Google!

| StackOverflow