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

Post on 30-Jun-2019

230 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to R and RStudio IDE

Wan Nor Arifin

Unit of Biostatistics and Research Methodology, Universiti Sains Malaysia.

email: wnarifin@usm.my

December 19, 2018

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 2 / 27

Why use R?

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

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

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

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

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

R and RStudio

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

R Installation

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

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

RStudio Installation

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

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

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

R Interface

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

RStudio Interface

Figure 3: RStudio

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

R script

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 15 / 27

Function and Object

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 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 December 19, 2018 18 / 27

R packages

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 20 / 27

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

Working directory

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 23 / 27

Help

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 25 / 27

Thank you

Wan Nor Arifin (USM) Introduction to R and RStudio IDE December 19, 2018 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 December 19, 2018 27 / 27

top related