introduction to r and rstudio ide · 3 rscript 4 functionandobject 5 rpackages 6 workingdirectory 7...

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

Upload: others

Post on 27-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

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

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

Why use R?

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 3 / 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

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

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

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

R and RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 8 / 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

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

Figure 5: RStudio

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 10 / 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

R Interface

Figure 7: Plain R

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

RStudio Interface

Figure 8: RStudio

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

R script

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 14 / 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

Function and Object

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 16 / 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

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

R packages

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 19 / 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

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

Working directory

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 22 / 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

Help

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 24 / 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

Thank you

Wan Nor Arifin (USM) Introduction to R and RStudio IDE February 13, 2019 26 / 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