1 installing & getting started with r

55
Dr Nisha Arora Getting started with R

Upload: naroranisha

Post on 14-Apr-2017

35 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: 1 Installing & getting started with R

Dr Nisha Arora

Getting started with R

Page 2: 1 Installing & getting started with R

Contents

2

Download & Install R

Download & install R Studio

Install Packages in R/ R Studio

Set or change working directory

Get help in R

Import & Export data sets

Shortcuts & Tips

References & Resources

Page 3: 1 Installing & getting started with R

Why R?

3

Page 4: 1 Installing & getting started with R

ALGORITHM

4

Word used by programmers when they do

not want to explain what they did !

Page 5: 1 Installing & getting started with R

5

The only programming JOKE, I know is

my code.

Just like that

Page 6: 1 Installing & getting started with R

6

Page 8: 1 Installing & getting started with R

8

To download & install R

Page 9: 1 Installing & getting started with R

9

To download & install R

Page 10: 1 Installing & getting started with R

10

To download & install R

Page 11: 1 Installing & getting started with R

11

To download & install R

Page 12: 1 Installing & getting started with R

12

To download & install R

Page 13: 1 Installing & getting started with R

13

To download & install R

Page 14: 1 Installing & getting started with R

14

To download & install R

Page 15: 1 Installing & getting started with R

15

To download & install R

Page 16: 1 Installing & getting started with R

16

To download & install R

Page 17: 1 Installing & getting started with R

17

To download & install R

Page 18: 1 Installing & getting started with R

18

To download & install R

Page 19: 1 Installing & getting started with R

19

To download & install R

Page 20: 1 Installing & getting started with R

20

R GUI

R studio

Includes a console, editor, as well as tools for plotting, history,

debugging and workspace management

R Commander

Enables analysts to access a selection of commonly-used R commands

using a simple interface

Rattle

A data mining toolkit which was designed to analyze large data sets

Deducer

Designed to be a easy to use alternative to proprietary data analysis

software such as SPSS and Minitab

Page 21: 1 Installing & getting started with R

21

Download & install R Studio

Open the URL: www.rstudio.com

Click on “Download Rstudio” button.

Click on "Download RStudio Desktop”.

Click on the version recommended for your system, or the

latest

Windows version, and save the executable file.

Run the .exe file and follow the installation instructions.

Page 22: 1 Installing & getting started with R

22

R packages

Base packages

Which come with R automatically

Contributed packages

Which must be downloaded for installation

To find the list of default base packages loaded at start-up

getOption("defaultPackages")

"datasets" "utils" "grDevices" "graphics" "stats" "methods"

Page 23: 1 Installing & getting started with R

23

To install packages in R

Page 24: 1 Installing & getting started with R

24

To install packages in R Studio

Page 25: 1 Installing & getting started with R

25

To install packages in R Studio

Page 26: 1 Installing & getting started with R

26

To install packages in R Studio

Page 27: 1 Installing & getting started with R

27

To install packages in R Studio

# To see all packages installed

library()

#To see packages currently loaded

search()

#To install a package ‘caret’

install.packages(“caret”, dependencies=TRUE)

#To load a package ‘caret’

library(caret)

#To discover the contents of library package ‘caret’

library(help=caret)

Page 28: 1 Installing & getting started with R

28

Working Directory

Page 29: 1 Installing & getting started with R

29

Working Directory

# To check the current working directory

getwd()

# To set a new working directory

setwd("D:\\NISHA\\R Coursera\\R_programming")

Note: use '\\' or '/' in the path of directory

# The shortcut (to get/change the current working directory)

"ctr+shift+h“

Page 30: 1 Installing & getting started with R

30

To get help in R

Click on the 'Help' menu.

If you want help on a specific command you can enter a search

directly from the keyboard: help(keyword), e.g., help(mean)

A shortcut is to type: ?keyword

If you are not sure of the command you can try the following:

apropos("part.word"), e.g., apropos(wd); apropos(“mean")

To invoke a built-in help browser, you can type: help.start()

Page 31: 1 Installing & getting started with R

Data Sets

31

#To see the list of available data sets in R

data()

#To load an available data sets in R

data(name_of_dataset)

#For example;

data(airquality)

Page 32: 1 Installing & getting started with R

Data Sets

33

Page 33: 1 Installing & getting started with R

Import Data Sets

34

#To import Text data file in R

myTEXTdata = read.table("mydata.txt“, header = TRUE)

Make sure to set your working directory

Page 34: 1 Installing & getting started with R

Import Data Sets

35

#To import CSV data file in R

myCSVdata <- read.table("c:/mydata.csv", header=TRUE,

sep=",", row.names="id")

# note the / instead of \ on Windows systems

Or myCSVdata = read.csv("mydata.csv")

Page 35: 1 Installing & getting started with R

Import Data Sets

36

#To import Excel data file in R

# read in the first worksheet from the workbook myexcel.xlsx

# first row contains variable names

library(xlsx)

mydata <- read.xlsx("c:/myexcel.xlsx", 1)

# read in the worksheet named mysheet

mydata <- read.xlsx("c:/myexcel.xlsx", sheetName = "mysheet")

Page 36: 1 Installing & getting started with R

Import Data Sets

37

#To import Excel data file in R

# read in the first worksheet from the workbook myexcel.xlsx

# first row contains variable names

library(readxl)

mydata <- read_excel("c:/myexcel.xlsx", sheet =1)

# read in the worksheet named mysheet

mydata <- read_excel("c:/myexcel.xlsx", sheet = "mysheet")

Page 37: 1 Installing & getting started with R

Import Data Sets

38

To import SPSS data file in R

library(foreign); mySPSSdata <- read.spss("Housing Pricing

Data.sav“)

If SPSS dataset is in trasport format

library(Hmisc); mySPSSdata <- spss.get("c:/mydata.por",

use.value.labels=TRUE)

Page 38: 1 Installing & getting started with R

Import Data Sets

39

#To import SAS data file in R

install.packages("sas7bdat"); library(sas7bdat)

mySASdata <- read.sas7bdat("d1.sas7bdat")

If SAS dataset is in trasport format

library(Hmisc); mySASdata <- sasxport.get("c:/mydata.xpt")

# character variables are converted to R factors

Page 39: 1 Installing & getting started with R

Import Data Sets

40

#To import STATA data file in R

install.packages(“foreign")

library(foreign)

mySTATAdata <- read.dta("D://Learning//Learn

R//R_programming data and codes//sales.dta")

Page 40: 1 Installing & getting started with R

Data Sets

41

Page 41: 1 Installing & getting started with R

Export Data Sets

42

#To export R data file to a tab delimited text file

write.table(mydata, "c:/mydata.txt", sep="\t")

Page 42: 1 Installing & getting started with R

Export Data Sets

43

#To export R data file CSV format

write.csv(MyData, file = "MyData.csv")

write.table(MyData, file = "MyData.csv",row.names=FALSE,

na="",col.names=FALSE, sep=",")

Page 43: 1 Installing & getting started with R

Export Data Sets

44

#To export R data file to an Excel spreadsheet

library(xlsx)

write.xlsx(mydata, "c:/mydata.xlsx")

Page 44: 1 Installing & getting started with R

Export Data Sets

45

To export R data file to SPSS

# write out text data file and an SPSS program to read it

library(foreign)

write.foreign(mydata, “D:/mydata.txt",

D:/mydata.sps", package="SPSS")

Page 45: 1 Installing & getting started with R

Export Data Sets

46

#To export R data file to SAS

# write out text datafile and an SAS program to read it

library(foreign)

write.foreign(mydata, "c:/mydata.txt",

"c:/mydata.sas", package="SAS")

Page 46: 1 Installing & getting started with R

Export Data Sets

47

#To export R data file to STATA

# export data frame to Stata binary format

library(foreign)

write.dta(mydata, "c:/mydata.dta")

Page 49: 1 Installing & getting started with R

Short-cuts

https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-

Shortcuts

50

Description Windows & Linux Mac

Goto File/Function Ctrl+. Ctrl+.

Move cursor to Source Editor Ctrl+1 Ctrl+1

New document (except on

Chrome/Windows) Ctrl+Shift+N Command+Shift+N

New document (Chrome only) Ctrl+Alt+Shift+N Command+Shift+Alt+N

Open document Ctrl+O Command+O

Save active document Ctrl+S Command+S

Close active document (except

on Chrome) Ctrl+W Command+W

Goto File/Function Ctrl+. Ctrl+.

Page 50: 1 Installing & getting started with R

51

Get your hands dirty

R & R Studio

Install & Load Packages

Arithmetic in R

Numbers in R (NAN & NA)

Import data to R

Export data from R

Descriptive Statistics

Basic Plots

Page 55: 1 Installing & getting started with R

Thank You