introduction to r and rstudio ide - wnarifin.github.io · introductiontorandrstudioide...

27
Introduction to R and RStudio IDE Wan Nor Arifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email: [email protected] December 19, 2018 Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 1 / 27

Upload: phamtuong

Post on 30-Jun-2019

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Introduction to R and RStudio IDE

Wan Nor Arifin

Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia.

email: [email protected]

December 19, 2018

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 1 / 27

Page 2: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 2 / 27

Page 3: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Why use R?

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 3 / 27

Page 4: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R

R is a free software environment for statistical computing andgraphics. It compiles and runs on a wide variety of UNIX platforms,Windows and MacOS.

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

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 4 / 27

Page 5: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R outpaces other statistics software

Source: http://r4stats.com/articles/popularity/

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 5 / 27

Page 6: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R is getting more popular among academician

Source: https://stackoverflow.blog/2017/10/10/impressive-growth-r/

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 6 / 27

Page 7: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Availability of R packages

Source: https://cloud.r-project.org/web/packages/index.html, as of18/12/2018.

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 7 / 27

Page 8: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R and RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 8 / 27

Page 9: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R Installation

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

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 9 / 27

Page 10: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

RStudio Installation

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

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 10 / 27

Page 11: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R to PDF Installation

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

Important for R-markdown session later.

Figure 1: miKTeX

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 11 / 27

Page 12: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R Interface

Figure 2: Plain RWan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 12 / 27

Page 13: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

RStudio Interface

Figure 3: RStudio

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

Page 14: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R script

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 14 / 27

Page 15: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 15 / 27

Page 16: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Function and Object

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 16 / 27

Page 17: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Function

R function(), think of MS Excel functionstructure

function(argument1 = value, argument2 = value)

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 17 / 27

Page 18: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 18 / 27

Page 19: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

R packages

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 19 / 27

Page 20: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 20 / 27

Page 21: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Load libraries

e.g. load psych and car,

library(psych)library(car)

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 21 / 27

Page 22: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Working directory

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 22 / 27

Page 23: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 23 / 27

Page 24: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Help

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 24 / 27

Page 25: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 25 / 27

Page 26: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

Thank you

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 26 / 27

Page 27: Introduction to R and RStudio IDE - wnarifin.github.io · IntroductiontoRandRStudioIDE WanNorArifin Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia. email:

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 December 19, 2018 27 / 27