R Programming

bsramar 90 views 145 slides May 22, 2018
Slide 1
Slide 1 of 145
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145

About This Presentation

R is a programming language and free software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing.


Slide Content

PROGRAMMING by Bs Ram

Why Do We Need Analytics ?

Why Do We Need Analytics? Data Analytics helps organizations harness their data and use it to identify new opportunities. DATA ANALYTICS Cost Reduction Faster, Better Decision Organization Analysis Better Marketing & Product Analysis

What is Business analytics? Business Analytics examines large and different types of data to uncover hidden patterns, correlations and other insights.

What is Data Visualization? Visualization allows us visual access to huge amounts of data in easily digestible visuals. Well designed data graphics are usually the simplest and at the same time, the most powerful.

Why R? Apart from being used as statistical language, it can also be used a programming language for analytical purposes. Apart from being one of the most dominant analytics tools, R also is one of the most popular tools used for data visualization Programming and Statistical Language Data Analysis and Visualization

Why R? R is a simple and easy to learn, read & write R is an example of a FLOSS (Free / Libre and Open Source Software) which means one can freely distribute copies of this software, read it’s source code, modify it, etc. Simple and Easy to Learn Free and Open Source Easy 2 Learn

R Installation Base https://cran.r-project.org/bin/windows/base / Download R 3.5.0 for Windows (62 megabytes, 32/64 bit) Windows Ubuntu Press Ctrl+Alt+T to open Terminal Then execute  sudo apt-get update   After that,  sudo apt-get install r-base

R Installation R Studio Windows Ubuntu sudo apt-get install gdebi -core wget https://download1.rstudio.org/rstudio-1.0.44-amd64.deb sudo gdebi rstudio-1.0.44-amd64.deb rm rstudio-1.0.44-amd64.deb https:// www.rstudio.com/products/rstudio/download /

Download Here

R Installation R Studio Windows Ubuntu sudo apt-get install gdebi -core wget https://download1.rstudio.org/rstudio-1.0.44-amd64.deb sudo gdebi rstudio-1.0.44-amd64.deb rm rstudio-1.0.44-amd64.deb https:// www.rstudio.com/products/rstudio/download /

Module

Variables Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. R Basics A = 2 S <- “AU TVS Centre for Quality Management” TURE -> Y Memory A = 2 S <- “AU TVS Centre for Quality Management” Y = TURE

Operator R Basics

Operator R Basics

Operator R Basics

Operator R Basics

Arithmetic Operators R Basics >2+3 5 >+2 >3-1 2 >-2 >2*3 6 >6/3 2 >5%%2 1 >7%/%3 2 >2^3 8

Arithmetic Operators R Basics

R Basics

Data Types R Basics

Data Types R Basics

Vectors R Basics

Vectors R Basics

Vectors : Sequence Operations R Basics

List R Basics

List Sequence operations R Basics

Array R Basics

Array R Basics

Matrices R Basics

Matrices R Basics

Matrices R Basics

Factors R Basics

Factors R Basics

Data Frames R Basics

Data Frames R Basics

Flow Control Statement R Basics

Flow Control R Basics

Flow Control Statement R Basics

Loops R Basics

Loops R Basics

Repeat Statement R Basics

While Statement R Basics

For Statement R Basics

For Statement R Basics

Brake Statement R Basics Brake;

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Data Visualization R Basics

Function

Function Syntax: functionName <- function(inputs) { body of the function which returns something} You must run/submit the function before you can use it Note that variable naming within a function is protected, and not altered by whatever your script is doing

Function # tests if a number if positive pos = function(x) { if(x >= 0) out = 1 if(x < 0) out = 0 return(out) } Input Output

Function > pos = function(x) { + if(x >= 0) out = 1 + if(x < 0) out = 0 + return(out) + } > pos (5) [1] 1 > pos (-5) [1] 0 Run the function first

Data Manipulation

Importing Data Into R

Open your file

Reading data from txt|csv files: R base functions R base functions for importing data:  read.table (),  read.delim (),  read.csv (),  read.csv2 () Reading a local file Reading a file from internet

Reading data from txt|csv files: R base functions

Fast Reading of Data From txt|csv Files into R: readr package Functions for reading txt|csv files:  read_delim (),  read_tsv (),  read_csv (), read_csv2() Reading a file Reading a local file Reading a file from internet In the case of parsing problems Specify column types Reading lines from a file:  read_lines () Read whole file:  read_file ()

Fast Reading of Data From txt|csv Files into R: readr package

Reading data From Excel Files ( xls|xlsx ) into R

Exporting Data From R

Exporting Data From R Export Data From R to txt|csv|Excel files Writing data from R to a txt|csv file: R base functions Fast Writing of Data From R to txt|csv Files: readr package Writing data from R to Excel files ( xls|xlsx ) Saving data into R data format: RDATA and RDS Create and format word and powerpoint documents using R and ReporteRs package : Create and format Word documents Create a Word document from a template file Add a table into a Word document Create and format PowerPoint documents Create an editable graph from R software to PowerPoint

Writing data from R to a txt|csv file: R base functions R R base functions for writing data:  write.table (),  write.csv (),  write.csv2 () Writing data to a file

Fast writing of Data From R to txt|csv Files: readr package   Installing and loading readr :  install.packages (“ readr ”) readr functions for writing data:  write_tsv (),  write_csv () Writing data to a file

Fast writing of Data From R to txt|csv Files: readr package  

Writing data from R to Excel files ( xls|xlsx )   Installing xlsx package:  install.packages (“ xlsx ”) Using xlsx package:  write.xlsx ()

Writing data from R to Excel files ( xls|xlsx )  

Saving data into R data format: RDATA and RDS   Save one object to a file:  saveRDS (object, file),  readRDS (file) Save multiple objects to a file:  save (data1, data2, file),  load (file) Save your entire workspace:  save.image (),  load ()

Saving data into R data format: RDATA and RDS   Saving and restoring one single R object:

Saving data into R data format: RDATA and RDS   2. Saving and restoring one or more R objects :

Saving data into R data format: RDATA and RDS   3. Saving and restoring your entire workspace

Create and format Word documents with R and ReporteRs package  ReporteRs  package, by David Gohel , provides easy to use functions to  write  and  format   Word documents . It can be also used to generate  Word document from a template file  with logos, fonts, etc. ReporteRs is Java-based solution, so it works on Windows, Linux and Mac OS systems. Install and load the ReporteRs R package Create a simple Word document Add texts : title and paragraphs of texts Format the text of a Word document using R software Add plots and images Add a table Add lists : ordered and unordered lists Add a footnote to a Word document Add R scripts Add a table of contents into a Word document

Create a Word document from a template file with R and ReporteRs package   Quick introduction to ReporteRs package Create a Word document using a template file data <- iris [ 1 : 5 , ] data

Add a simple table

Create a Word document from a template file with R and ReporteRs package   Quick introduction to ReporteRs package Create a Word document using a template file data <- iris [ 1 : 5 , ] data

Packages

dplyr data.table ggplot2 reshape2 readr tidyr lubridate Packages ggplot2 is a graphical package. But, it generally helps in visualizing data ( distributions, correlations) and making manipulations accordingly. 

dplyr Package by  Hadley Wickham .  filter – It filters the data based on a condition select – It is used to select columns of interest from a data set arrange – It is used to arrange data set values on ascending or descending order mutate – It is used to create new variables from existing variables summarise (with group_by ) – It is used to perform analysis by commonly used operations such as min, max, mean count etc This package has everything (almost) to accelerate your data manipulation efforts. It is known best for data exploration and transformation. It’s chaining syntax makes it highly adaptive to use. 

dplyr Package by  Hadley Wickham .  I have used 2 pre-installed R data sets namely mtcars and iris.

dplyr Package by  Hadley Wickham .  #read data > head( mydata )

dplyr Package by  Hadley Wickham .  #creating a local dataframe . Local data frame are easier to read

dplyr Package by  Hadley Wickham .  #now data will be in tabular structure > mynewdata

dplyr Package by  Hadley Wickham .  > myirisdata

dplyr Package by  Hadley Wickham .  #use filter to filter data with required condition filter( mynewdata , cyl > 4 & gear > 4 ) filter

dplyr Package by  Hadley Wickham .  #use filter to filter data with required condition filter( mynewdata , cyl > 4)

dplyr Package by  Hadley Wickham .  > filter( myirisdata , Species %in% c(' setosa ', ' virginica '))

dplyr Package by  Hadley Wickham .  >select( mynewdata , cyl,mpg,hp ) #use select to pick columns by name select

dplyr Package by  Hadley Wickham .  > select( mynewdata , - cyl , -mpg ) #here you can use (-) to hide columns

dplyr Package by  Hadley Wickham .  #hide a range of columns > select( mynewdata , -c( cyl,mpg ))

dplyr Package by  Hadley Wickham .  #select series of columns > select( mynewdata , cyl:gear )

dplyr Package by  Hadley Wickham .  #chaining or pipelining - a way to perform multiple operations mynewdata %>% select( cyl , wt , gear) %>% filter( wt > 2)

dplyr Package by  Hadley Wickham .  #arrange can be used to reorder rows > mynewdata %>% select( cyl , wt , gear)%>% arrange( wt )

dplyr Package by  Hadley Wickham .  #Or > mynewdata %>% select( cyl , wt , gear)%>% arrange( desc ( wt ))

dplyr Package by  Hadley Wickham .  New variable > newvariable <- mynewdata %>% mutate( newvariable = mpg* cyl ) this is used to find insights from data > myirisdata %>% group_by (Species)%>% summarise (Average = mean( Sepal.Length , na.rm = TRUE)) Summarise -

dplyr Package by  Hadley Wickham .  > myirisdata %>% group_by (Species )%>% summarise_each (funs(mean , n()), Sepal.Length , Sepal.Width )

dplyr Package by  Hadley Wickham .  # you can rename the variables using rename command > mynewdata %>% rename(miles = mpg) Rename

data.table Package Leave your traditional ways of sub setting rows and columns and use this package. With minimum coding, you can do much more. Using data.table helps in reducing computing time as compared to data.frame . #load data > data (" airquality ") > mydata <- airquality > head(airquality,6) --- > data(iris ) > myiris <- iris

data.table Package # load package > library( data.table ) > mydata <- data.table ( mydata ) > mydata

data.table Package > myiris <- data.table ( myiris ) > myiris

data.table Package #subset rows - select 2nd to 4th row > mydata [2:4,]

data.table Package # select columns with particular values > > myiris [Species == ' setosa ']

data.table Package #select columns with multiple values. This will give you columns with Setosa # and virginica species > myiris [Species %in% c(' setosa ', ' virginica ')] # select columns. Returns a vector > mydata [,Temp]

data.table Package > mydata [,.( Temp,Month )] #returns sum of selected column > mydata [,sum(Ozone, na.rm = TRUE)] [ 1]4887 #returns sum and standard deviation > mydata [,.(sum(Ozone, na.rm = TRUE), sd (Ozone, na.rm = TRUE))]

data.table Package # print and plot > myiris [,{print( Sepal.Length ) > plot( Sepal.Width ) NULL}]

data.table Package # print and plot > myiris [,{print( Sepal.Length ) > plot( Sepal.Width ) NULL}]

data.table Package #grouping by a variable > myiris [,.( sepalsum = sum( Sepal.Length )), by=Species]

ggplot2 Package ggplot offers a whole new world of colors and patterns. If you are a creative soul, you would love this package till depth . Chart patterns covers almost every type of data representation except maps. ggplot is enriched with customized features to make your visualization better and better. > library(ggplot2 ) > library( gridExtra ) > df <- ToothGrowth

ggplot2 Package > df$dose <- as.factor ( df$dose ) > head( df )

ggplot2 Package #boxplot > bp <- ggplot ( df , aes (x = dose, y = len , color = dose)) + geom_boxplot () + theme( legend.position = 'none') > bp

ggplot2 Package # add gridlines > bp + background_grid (major = " xy ", minor = 'none ') > bp

ggplot2 Package # scatterplot > sp <- ggplot (mpg, aes (x = cty , y = hwy , color = factor( cyl )))+ geom_point (size = 2.5) > sp

ggplot2 Package # barplot > bp <- ggplot (diamonds, aes (clarity, fill = cut)) + geom_bar () +theme( axis.text.x = element_text (angle = 70, vjust = 0.5)) > bp

ggplot2 Package # compare two plots > plot_grid ( sp , bp , labels = c("A","B"), ncol = 2, nrow = 1 )

ggplot2 Package #histogram > ggplot (diamonds, aes (x = carat)) + geom_histogram ( binwidth = 0.25, fill = ' steelblue ')+ scale_x_continuous (breaks= seq (0,3, by=0.5))

reshape2 Package this package is useful in reshaping data R base functions consist of ‘Aggregation’ option using which data can be reduced and rearranged into smaller forms, but with reduction in amount of information. Aggregation includes tapply , by and aggregate base functions. The reshape package overcome these problems.  melt  This function converts data from wide format to long format. It’s a form of restructuring where multiple categorical columns are ‘melted’ into unique rows.

reshape2 Package #create a data > ID <- c(1,2,3,4,5) > Names <- c('Joseph',' Matrin ','Joseph','James',' Matrin ') > DateofBirth <- c(1993,1992,1993,1994,1992) > Subject<- c(' Maths ','Biology','Science',' Psycology ','Physics') > thisdata <- data.frame (ID, Names, DateofBirth , Subject) > data.table ( thisdata )

reshape2 Package #load package > install.packages ('reshape2') > library(reshape2) #melt > mt <- melt( thisdata , id=(c(' ID','Names '))) > mt

reshape2 Package cast  : This function converts data from long format to wide format. It starts with melted data and reshapes into long format. It’s just the reverse of  melt  function. It has two functions namely,  dcast  and  acast . dcast returns a data frame as output. acast returns a vector/matrix/array as the output. #cast > mcast <- dcast ( mt , DateofBirth + Subject ~ variable) > mcast

readr Package ‘ readr ’ helps in reading various forms of data into R. With 10x faster speed. Here, characters are never converted to factors(so no more stringAsFactors = FALSE). This package can replace the traditional read.csv() and read.table () base R functions . Delimited files withread_delim (), read_csv (), read_tsv (), andread_csv2(). Fixed width files with read_fwf (), and read_table (). Web log files with read_log () > install.packages (' readr ') > library( readr ) > read_csv ('test.csv', col_names = TRUE)

readr Package > read_csv ("iris.csv", col_types = list( Sepal.Length = col_double (), Sepal.Width = col_double (), Petal.Length = col_double (), Petal.Width = col_double (), Species = col_factor (c(" setosa ", " versicolor ", " virginica ")) )) > read_csv ("iris.csv", col_types = list( Species = col_factor (c(" setosa ", " versicolor ", " virginica ")) )

tidyr Package This package can make your data look ‘tidy’. It has 4 major functions to accomplish this task. Needless to say, if you find yourself stuck in data exploration phase, you can use them anytime (along with dplyr ).  gather() – it ‘gathers’ multiple columns. Then, it converts them into key:value pairs. This function will transform wide from of data to long form. You can use it as in alternative to ‘melt’ in reshape package. spread() – It does reverse of gather. It takes a key:value pair and converts it into separate columns. separate() – It splits a column into multiple columns. unite() – It does reverse of separate. It unites multiple columns into single column

tidyr Package #load package > library( tidyr ) #create a dummy data set > names <- c('A','B','C','D','E','A','B') > weight <- c(55,49,76,71,65,44,34) > age <- c(21,20,25,29,33,32,38) > Class <- c('Maths','Science','Social','Physics','Biology','Economics','Accounts')

tidyr Package #create data frame > tdata <- data.frame (names, age, weight, Class) > tdata

tidyr Package #using gather function > long_t <- tdata %>% gather(Key, Value, weight:Class ) > long_t

tidyr Package #create a data set > Humidity <- c(37.79, 42.34, 52.16, 44.57, 43.83, 44.59) > Rain <- c(0.971360441, 1.10969716, 1.064475853, 0.953183435, 0.98878849, 0.939676146) > Time <- c("27/01/2015 15:44","23/02/2015 23:24", "31/03/2015 19:15", "20/01/2015 20:52", "23/02/2015 07:46", "31/01/2015 01:55 ") #build a data frame > d_set <- data.frame (Humidity, Rain, Time) #using separate function we can separate date, month, year > separate_d <- d_set %>% separate(Time, c('Date', ' Month','Year ')) > separate_d

tidyr Package # using unite function - reverse of separate > unite_d <- separate_d %>% unite(Time, c(Date, Month, Year), sep = "/") > unite_d

tidyr Package #using spread function - reverse of gather > wide_t <- long_t %>% spread(Key, Value) > wide_t

Lubridate Package Lubridate package reduces the pain of working of data time variable in R. The inbuilt function of this package offers a nice way to make easy parsing in dates and times . > install.packages (' lubridate ') > library( lubridate ) #current date and time > now () [ 1] "2015-12-11 13:23:48 IST“ #assigning current date and time to variable n_time > n_time <- now()

Lubridate Package #using update function > n_update <- update( n_time , year = 2013, month = 10) > n_update [ 1] "2013-10-11 13:24:28 IST" #add days, months, year, seconds > d_time <- now() > d_time + ddays (1) [1] "2015-12-12 13:24:54 IST" > d_time + dweeks (2) [1] "2015-12-12 13:24:54 IST" > d_time + dyears (3) [1] "2018-12-10 13:24:54 IST" > d_time + dhours (2) [1] "2015-12-11 15:24:54 IST" > d_time + dminutes (50) [1] "2015-12-11 14:14:54 IST" > d_time + dseconds (60) [1] "2015-12-11 13:25:54 IST" #extract date,time > n_time$hour <- hour(now()) > n_time$minute <- minute(now()) > n_time$second <- second(now()) > n_time$month <- month(now()) > n_time$year <- year(now())

Lubridate Package #check the extracted dates in separate columns > new_data <- data.frame ( n_time$hour , n_time$minute , n_time$second , n_time$month , n_time$year ) > new_data

Thank you! PROGRAMMING by Bs Ram