Top Banner
Introduction to R and RStudio IDE Wan Nor Arifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. Email: [email protected] February 13, 2019 Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 1 / 27
27

Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Mar 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Introduction to R and RStudio IDE

Wan Nor Arifin

Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia.

Email: [email protected]

February 13, 2019

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 1 / 27

Page 2: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

1 Why use R?

2 R and RStudio

3 R script

4 Function and Object

5 R packages

6 Working directory

7 Help

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 2 / 27

Page 3: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Why use R?

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 3 / 27

Page 4: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R

R is a free software environment for statisticalcomputing and graphics. It compiles and runs on a widevariety of UNIX platforms, Windows and MacOS.

Source: https://www.r-project.org/

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 4 / 27

Page 5: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R outpaces other statistics software

Figure 1: R popularity

Source: http://r4stats.com/articles/popularity/Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 5 / 27

Page 6: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R is getting more popular among academician

Figure 2: Impressive growth of R

Source: https://stackoverflow.blog/2017/10/10/impressive-growth-r/Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 6 / 27

Page 7: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Availability of R packages

Figure 3: CRAN

Source: https://cloud.r-project.org/web/packages/index.html, as of 11/2/2019.Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 7 / 27

Page 8: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R and RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 8 / 27

Page 9: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R InstallationR @ https://cran.r-project.org/

Figure 4: R Project

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 9 / 27

Page 10: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

RStudio InstallationRStudio @ http://www.rstudio.com/

Figure 5: RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 10 / 27

Page 11: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R to PDF Installation

Windows & MacOS – MikTeX @ https://miktex.org/downloadLinux – texlive.

Important for R-markdown session later.

Figure 6: MiKTeXWan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 11 / 27

Page 12: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R Interface

Figure 7: Plain R

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 12 / 27

Page 13: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

RStudio Interface

Figure 8: RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 13 / 27

Page 14: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R script

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 14 / 27

Page 15: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Script panel

File > New file > R script

type all commands/functions herecomments, start with “#”run all commands by Ctrl+Enter

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 15 / 27

Page 16: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Function and Object

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 16 / 27

Page 17: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Function

R function(), think of MS Excel functionstructure

function(argument1 = value, argument2 = value)

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 17 / 27

Page 18: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Object

name assigned on left side of “<-” / “=”variable, data (data frame, matrix, list)

x <- 1y = 2z = x + yz # type object name, you'll get the value

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 18 / 27

Page 19: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

R packages

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 19 / 27

Page 20: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Install packages a.k.a libraries

Graphically

Packages > Install

Command

e.g. psych, car

install.packages("psych")install.packages("car")

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 20 / 27

Page 21: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Load libraries

e.g. load psych and car,

library(psych)library(car)

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 21 / 27

Page 22: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Working directory

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 22 / 27

Page 23: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Set working directory

1 Browse to target directory: Files Tab > . . . > Go To Folder2 Files Tab > More > Set As Working Directory

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 23 / 27

Page 24: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Help

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 24 / 27

Page 25: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Help

If you know the name of packages/functions,

?psych?library

Search by keywords,

??mean??survey

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 25 / 27

Page 26: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

Thank you

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 26 / 27

Page 27: Introduction to R and RStudio IDE · 3 Rscript 4 FunctionandObject 5 Rpackages 6 Workingdirectory 7 Help WanNorArifin (USM) IntroductiontoRandRStudioIDE February13,2019 2/27. ...

References

Fox, J., Weisberg, S., & Price, B. (2018). Car: Companion to appliedregression. Retrieved from https://CRAN.R-project.org/package=car

R Core Team. (2018). R: A language and environment for statisticalcomputing. Vienna, Austria: R Foundation for Statistical Computing.Retrieved from https://www.R-project.org/

Revelle, W. (2018). Psych: Procedures for psychological, psychometric, andpersonality research. Retrieved fromhttps://CRAN.R-project.org/package=psych

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 27 / 27