r introduction - daviddalpiaz.github.io · r introduction author: david dalpiaz created date:...

15
R Introduction David Dalpiaz STAT 430, Fall 2017 1

Upload: others

Post on 16-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

R Introduction

David DalpiazSTAT 430, Fall 2017

1

Page 2: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

What the heck is R?

• R is a programming language designed specifically for statisticalcomputing and graphics.

• R is an interactive interface to many different tools.• R is based on the S language, which was developed by Bell

laboratories.• R is free, specifically open source.

• “Think free as in free speech, not free beer.”• R and RStudio are actually both.

• R is cool.

2

Page 3: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Learning is HARD!

3

Page 4: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Help?

The only way to learn R (or anything), is to fail. Then, get help.

• ?name_of_thing• Google!!!• Stack Overflow• R Resources• Ask!

4

Page 5: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

RStudio

5

Page 6: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Know thine RStudio Environment

There are a lot of keyboard shortcuts in RStudio. These shortcutsare meant to speed up your work.

To view all the options, you must engage the keyboard shortcut thatrules them all:

• Windows: Alt + Shift + K• macOS: Option + Shift + K

6

Page 7: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Some Favorites

1. Autocomplete command• Both: Tab

2. Runs the current line or selection from the editor to the consoleand runs it

• Windows: Ctrl + Enter• macOS: Cmd + Enter

3. Comment multiple lines.• Windows: Ctrl + Shift + C• macOS: Command + Shift + C

4. “Reformat”" Code:• Windows: Ctrl + Shift + A• macOS: Command + Shift + A

More to come when we introduce rmarkdown. . .7

Page 8: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Objects and Functions

To understand computations in R, two slogans are helpful:Everything that exists is an object. Everything thathappens is a function call. — John Chambers

And when we say everything, we mean everything. Even a functionis an object.

8

Page 9: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Assignment

<- or = ?That is the question.

9

Page 10: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Attach

attach(dataset)

Do not ever do this.You might curse me now, but you’ll thank me later.

10

Page 11: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Vectors

• There is no scalar.• Be aware of vectorization!

11

Page 12: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Packages

Use them!They are what makes R great.

12

Page 13: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Style

Have one!

• Spaces around binary operators!• Indent two spaces!• Pick an assignment operator and stick to it.• Get some inspiration from Hadley or Google.

Be consistent!

• With yourself!• With your group!• With your company!

13

Page 14: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Next Time

• Lists?• Importing Data?• Frames vs Tibbles?• rmarkdown!

14

Page 15: R Introduction - daviddalpiaz.github.io · R Introduction Author: David Dalpiaz Created Date: 8/30/2017 10:01:45 AM

Now

Lab!

15