+ All Categories
Home > Documents > Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen...

Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen...

Date post: 31-Dec-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
28
Getting Started with R Tony Yao-Jen Kuo
Transcript
Page 1: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Getting Started with R

Tony Yao-Jen Kuo

Page 2: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Getting Started

Page 3: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

The characteristics of R

I Comparing to Matlab, SAS, SPSS: FreeI Comparing to Julia: Larger communityI Functional Programming

Page 4: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

R in one sentence

Applying functions to objects.

Page 5: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

R: core of computationI Download from https://cran.r-project.org/

Figure 1:

Page 6: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

RStudio: the IDE

I Download fromhttps://www.rstudio.com/products/rstudio/download

Page 7: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Interface of RStudio

Page 8: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Four blocks

I ScriptI ConsoleI EnvironmentI Multi-functional

Page 9: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Console tricks

I Using Ctrl + L for clearingI Using Up and Down arrow checking executed scripts

Page 10: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Keyboard shortcuts

I Using Alt + Shift + K for shortcuts

Page 11: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Quickstart

Page 12: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Using <- for assignment

I = is OKAY, but <- is more commonI Using Alt + - to get <-

Page 13: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Using # for comments

# Declaring objects

# Declaring function

Page 14: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Assignment in action

# Declaring objectsmy_favorite_star <- "Tom Cruise"my_lucky_number <- 24r_is_easy <- TRUE

# Declaring functionsay_hello <- function(){

return("Hello R!")}

Page 15: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Printing objects or calling function

# Printing objectsmy_favorite_starmy_lucky_numberr_is_easy

# Calling functionsay_hello()

## [1] "Tom Cruise"## [1] 24## [1] TRUE## [1] "Hello R!"

Page 16: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Using rm() to remove objects

rm(r_is_easy)r_is_easy # Error

Page 17: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Why is R Console showing +?

I The reason is that R is still expecting inputs from us

my_favorite_player <- "Steve Nash

say_hello <- function(){return("Hello R!")

help(print

Page 18: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

2 ways to solve it

1. Complete your inputs2. ESC

Page 19: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Install packages

I install.pacakges()I Do it once

Page 20: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Library packages

I library()I Do it every time

Page 21: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Useful functions

Page 22: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to query functions or data

help(print) # ?printhelp(cars) # ?cars

Page 23: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to show relative informationsessionInfo()

## R version 3.4.4 (2018-03-15)## Platform: x86_64-apple-darwin15.6.0 (64-bit)## Running under: macOS High Sierra 10.13.6#### Matrix products: default## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib#### locale:## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8#### attached base packages:## [1] stats graphics grDevices utils datasets methods base#### loaded via a namespace (and not attached):## [1] compiler_3.4.4 backports_1.1.2 magrittr_1.5 rprojroot_1.3-2## [5] tools_3.4.4 htmltools_0.3.6 yaml_2.1.19 Rcpp_0.12.16## [9] stringi_1.2.4 rmarkdown_1.9 knitr_1.20 stringr_1.3.1## [13] digest_0.6.15 evaluate_0.11

Page 24: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to get locale

Sys.getlocale()

## [1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"

Page 25: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to get current working directory

getwd()

## [1] "/Users/kuoyaojen/r_programming"

Page 26: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to set working directory

I Always use forward slash when declaring pathI Back slash has other functionalities, such as escape, Unicode. . .I Try not to use non-English username

setwd("/Users/USERNAME/Desktop") # MacOS desktopsetwd("C:/Users/USERNAME/Desktop") # Windows desktop

Page 27: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Function to quit RStudio

I Not saving workspace image is recommended

q()

Page 28: Getting Started with R - Yao-Jen Kuo · 2019. 3. 6. · Getting Started with R Author: Tony Yao-Jen Kuo Created Date: 9/1/2018 11:59:30 AM ...

Common learning path for programming

I Installation of compiler or interpreterI Choosing a friendly IDEI QuickstartI Variable typeI Control flowI Data structureI IterationI FunctionI (Optional) ClassI (Optional) Package


Recommended