1 r tutorial introduction

33
R Programming Sakthi Dasan Sekar http://shakthydoss.com 1

Upload: sakthi-dasans

Post on 14-Apr-2017

390 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: 1 R Tutorial Introduction

R ProgrammingSakthi Dasan Sekar

http://shakthydoss.com 1

Page 2: 1 R Tutorial Introduction

Introduction to R

R is a programming language developed as an alternative for Slanguage at AT&T Bell Laboratories by Robert Gentleman and RossIhaka.

Due to its underlying philosophy and design R is incredibly excel instatistical computation and graphic visualization.

R is free, open source with a very high activity community members.

Available cross all platforms (Linux, Mac, Windows)

http://shakthydoss.com 2

Page 3: 1 R Tutorial Introduction

Introduction to R

R is much more than just its core language. It has a worldwide repository system, the Comprehensive R Archive Network (CRAN) http://cran.r-project.org

As of 2011, there were more than 3,000 such packages hosted on CRAN and numerous more on other sites

http://shakthydoss.com 3

Page 4: 1 R Tutorial Introduction

Introduction to R

Cons of R Language

R has a steep learning curve.

Working with large datasets is limited by RAM size.

http://shakthydoss.com 4

Page 5: 1 R Tutorial Introduction

Introduction to R

Who use R in their business

http://shakthydoss.com 5

Page 6: 1 R Tutorial Introduction

Introduction to R

Understanding of R language

• R Statement or Commands can be separated by a semicolon (;) or a newline.

• Assignment operator in R is "<-" (although "=" also works).

• All characters after # are treated as comments.

• There is not multi line or block level Comments.

• $ (dollar) operator in R is an analogous to .(dot) operator in other languages. Student$name

Student$age

http://shakthydoss.com 6

Page 7: 1 R Tutorial Introduction

Introduction to R

Installing R

Installing from CRAN (Comprehensive R Network)

Website: http://cran.r-project.org/

R is actively being improved all the time, Make sure that you choose recent version to install.

http://shakthydoss.com 7

Page 8: 1 R Tutorial Introduction

Introduction to R

Installing R in windows

For Windows and Mac OS X, one can simply download a self-installing binary.

For Linux, installation varies. For the Debian distribution (including Ubuntu), R system can be installed using the regular package-management tools.

Since R is open source, one can also compile and install it using the source code.

http://shakthydoss.com 8

Page 9: 1 R Tutorial Introduction

Introduction to R

Installing R in windows

Window R package can be downloaded from

https://cran.r-project.org/bin/windows/base/

Download R 3.X.X for Windows executable file (.exe)

http://shakthydoss.com 9

Page 10: 1 R Tutorial Introduction

Introduction to R

Click Next in the Setup Wizard.

You can leave to setting to default.

http://shakthydoss.com 10

Page 11: 1 R Tutorial Introduction

Introduction to R

Open R Console (Rgui from your desktop) R Console

http://shakthydoss.com 11

Page 12: 1 R Tutorial Introduction

Introduction to R

IDE for R

The RStudio project currently provides most of the desired features for an IDE in a novel way, making it easier and more productive to use R.

The RStudio program can be run on the desktop or through a web browser.

The desktop version is available for Windows, Mac OS X, and Linux platforms.

http://shakthydoss.com 12

Page 13: 1 R Tutorial Introduction

Introduction to R

Notable IDEs for R

Name Platforms

ESS Windows, Mac, Linux

Eclipse Windows, Mac, Linux

SciViews Windows, Mac, Linux

JGR Windows, Mac, Linux

Tinn-R Windows

Notepad++ Windows

Rgui Windows

http://shakthydoss.com 13

Page 14: 1 R Tutorial Introduction

Introduction to R

Installing RStudio is usually a straightforward process.

For Windows and Mac OS X, one can simply download a self-installing binary.

For Linux, installation varies.

For the Debian distribution (including Ubuntu), RStudio can be installed using the regular package-management tools.

http://shakthydoss.com 14

Page 15: 1 R Tutorial Introduction

Introduction to R

Installing RStudio in windows.

https://www.rstudio.com/products/rstudio/download/

Download Rstudio.

Run the installation file.

http://shakthydoss.com 15

Page 16: 1 R Tutorial Introduction

Introduction to R

Open RStudio

Source Panel

R console

Environment Panel

File/PackagePanel

http://shakthydoss.com 16

Page 17: 1 R Tutorial Introduction

Introduction to R

R workspace

When R is started, it follows this process

R is started in the working directory (aka workspace).

If present, the .Rprofile file’s commands are executed.

If present, the .Rdata file is loaded.

http://shakthydoss.com 17

Page 18: 1 R Tutorial Introduction

Introduction to R

R workspace

R stores user-defined objects in workspace.

At the end of an R session, the user can save an snapshot of the current workspace and reloaded automatically next time when R is started.

getwd() - return working directory

setwd() - set working directory

http://shakthydoss.com 18

Page 19: 1 R Tutorial Introduction

Introduction to R

In RStudio you can set the workspace by Tools -> Global Options

http://shakthydoss.com 19

Page 20: 1 R Tutorial Introduction

Introduction to R

• There are over 1,000 functions at the core of R, and new R functions are created all of the time.

• Each R function comes with its own help page, which you can access by typing the function’s name after question mark

Getting Help

help.start() # Load HTML help pages into browser

help(package) # List help page for "package"

?package # Shortform for "help(package)"

help.search("keyword") # Search help pages for "keyword"

?help # For more options

help(package=base) # List tasks in package "base"

http://shakthydoss.com 20

Page 21: 1 R Tutorial Introduction

Introduction to R

Packages • Many Data Scientist programmers and statisticians , use R to design tools that can help people analyze data. Many of these

people contribute their code as preassembled collections of functions and objects called packages.

• Each R package is hosted at http://cran.r-project.org

• Not all packages are loaded by default, but can be load / install on demand

library() # List available packages to load

library("package") # loads the package

library(help="package") # list package contents

detach("package:pkg") # Unload the loaded package "pkg"

install.packages(“package") # Install the package

http://shakthydoss.com 21

Page 22: 1 R Tutorial Introduction

Introduction to R

Installing package by GUI RStudio -> Tools -> Install Packages

Check the package to load

http://shakthydoss.com 22

Page 23: 1 R Tutorial Introduction

Introduction to R

Knowledge Check

http://shakthydoss.com 23

Page 24: 1 R Tutorial Introduction

Introduction to R

Which of the statement is TRUE.

A. R is a propitiatory general purpose language

B. R excels in statically computation and graphic capabilities.

C. R syntax are very similar to assembly language.

D. R has no GUI

Answer B

http://shakthydoss.com 24

Page 25: 1 R Tutorial Introduction

Introduction to R

Which of the statement is TRUE.

A. Working with large datasets is limited by RAM size.

B. R language is own by AT&T Bell Labs

C. R available only for Unix platfroms

D. R language is design for parallel programming and horizontally scalable systems.

Answer A

http://shakthydoss.com 25

Page 26: 1 R Tutorial Introduction

Introduction to R

What is CRAN stands for

A. Comprehensive R Network

B. Computational R Network

C. Comprehensive R Archive Network

D. Computational R Archive Network

Answer C

http://shakthydoss.com 26

Page 27: 1 R Tutorial Introduction

Introduction to R

Which not an IDE for R language

A. RStudio

B. RGUI

C. Tinn-R

D. Dreamweaver

Answer D

http://shakthydoss.com 27

Page 28: 1 R Tutorial Introduction

Introduction to R

When R is started.

A. It loads .Rprofile and .Rdata from workspace

B. It attempts loads only .Rdata from workspace

C. It attempts loads only .Rprofile from workspace

D. Does nothing

Answer A

http://shakthydoss.com 28

Page 29: 1 R Tutorial Introduction

Introduction to R

Function getwd() - return working directory

A. TRUE

B. FALSE

Answer A

http://shakthydoss.com 29

Page 30: 1 R Tutorial Introduction

Introduction to R

Function setwd() - set working directory

A. TRUE

B. FALSE

Answer A

http://shakthydoss.com 30

Page 31: 1 R Tutorial Introduction

Introduction to R

How to browser for all help documents in R

A. help.start()

B. help(help)

C. library()

D. install.packages(help)

Answer A

http://shakthydoss.com 31

Page 32: 1 R Tutorial Introduction

Introduction to R

How to install new packages in R.

A. Install(“package”)

B. library(“package”)

C. install.packages("package")

D. load(“package”)

Answer C

http://shakthydoss.com 32

Page 33: 1 R Tutorial Introduction

Introduction to R

How to load a installed package.

A. load("package")

B. library("package")

C. install.packages("package")

D. library()

Answer B

http://shakthydoss.com 33