SlideShare a Scribd company logo
1 of 44
Download to read offline
1
Agenda
what are pipes?
why use pipes?
what are the different types of pipes?
combining operations with pipes
case studies
•
•
•
•
•
2
Introduction
R code contain a lot of parentheses in case of a sequence of multiple operations. When you are dealing with
complex code, it results in nested function calls which are hard to read and maintain. The package by
provides pipes enabling us to write R code that is readable.
Pipes allow us to clearly express a sequence of multiple operations by:
structuring operations from left to right
avoiding
nested function calls
intermediate steps
overwriting of original data
minimizing creation of local variables
magrittr
Stefan Milton Bache
•
•
•
•
•
•
3
Pipes
If you are using , magrittr will be automatically loaded. We will look at 3 different types of pipes:
%>% : pipe a value forward into an expression or function call
%<>%: result assigned to left hand side object instead of returning it
%$% : expose names within left hand side objects to right hand side expressions
tidyverse
•
•
•
4
Libraries
library(magrittr)
library(readr)
library(purrr)
library(dplyr)
library(stringr)
5
Data
## # A tibble: 1,000 x 4
## referrer n_pages duration purchase
## <fct> <dbl> <dbl> <lgl>
## 1 google 1 693 FALSE
## 2 yahoo 1 459 FALSE
## 3 direct 1 996 FALSE
## 4 bing 18 468 TRUE
## 5 yahoo 1 955 FALSE
## 6 yahoo 5 135 FALSE
## 7 yahoo 1 75 FALSE
## 8 direct 1 908 FALSE
## 9 bing 19 209 FALSE
## 10 google 1 208 FALSE
## # ... with 990 more rows
6
Data Dictionary
referrer: referrer website/search engine
n_pages: number of pages visited
duration: time spent on the website (in seconds)
purchase: whether visitor purchased
•
•
•
•
7
Sample Data
ecom_mini <- sample_n(ecom, size = 10)
8
head
head(ecom, 10)
## # A tibble: 10 x 4
## referrer n_pages duration purchase
## <fct> <dbl> <dbl> <lgl>
## 1 google 1 693 FALSE
## 2 yahoo 1 459 FALSE
## 3 direct 1 996 FALSE
## 4 bing 18 468 TRUE
## 5 yahoo 1 955 FALSE
## 6 yahoo 5 135 FALSE
## 7 yahoo 1 75 FALSE
## 8 direct 1 908 FALSE
## 9 bing 19 209 FALSE
## 10 google 1 208 FALSE
9
Using pipe
ecom %>% head(10)
## # A tibble: 10 x 4
## referrer n_pages duration purchase
## <fct> <dbl> <dbl> <lgl>
## 1 google 1 693 FALSE
## 2 yahoo 1 459 FALSE
## 3 direct 1 996 FALSE
## 4 bing 18 468 TRUE
## 5 yahoo 1 955 FALSE
## 6 yahoo 5 135 FALSE
## 7 yahoo 1 75 FALSE
## 8 direct 1 908 FALSE
## 9 bing 19 209 FALSE
## 10 google 1 208 FALSE
10
Square Root
y <- ecom_mini$n_pages
y <- sqrt(y)
# combine above steps
sqrt(ecom_mini$n_pages)
## [1] 2.236068 4.123106 4.242641 2.236068 4.000000 2.449490 3.605551
## [8] 3.162278 3.741657 1.000000
11
Square Root - Using pipe
# select n_pages variable and assign it to y
ecom_mini %$%
n_pages -> y
# compute square root of y and assign it to y
y %<>% sqrt()
12
13
Square Root - Using pipe
ecom_mini %$%
n_pages %>%
sqrt() -> y
y
## [1] 2.236068 4.123106 4.242641 2.236068 4.000000 2.449490 3.605551
## [8] 3.162278 3.741657 1.000000
14
15
Correlation
# without pipe
ecom1 <- subset(ecom, purchase)
cor(ecom1$n_pages, ecom1$duration)
## [1] 0.4290905
16
Correlation - Using pipe
# with pipe
ecom %>%
subset(purchase) %$%
cor(n_pages, duration)
## [1] 0.4290905
# using filter from dplyr and pipe
ecom %>%
filter(purchase) %$%
cor(n_pages, duration)
## [1] 0.4290905
17
Visualization
barplot(table(subset(ecom, purchase)$referrer))
18
19
Visualization - Using pipe
ecom %>%
subset(purchase) %>%
extract('referrer') %>%
table() %>%
barplot()
20
Regression
summary(lm(duration ~ n_pages, data = ecom))
##
## Call:
## lm(formula = duration ~ n_pages, data = ecom)
##
## Residuals:
## Min 1Q Median 3Q Max
## -386.45 -213.03 -38.93 179.31 602.55
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 404.803 11.323 35.750 < 2e-16 ***
## n_pages -8.355 1.296 -6.449 1.76e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 263.3 on 998 degrees of freedom
## Multiple R-squared: 0.04, Adjusted R-squared: 0.03904
## F-statistic: 41 58 on 1 and 998 DF p-value: 1 756e-10
21
Regression - Using pipe
ecom %$%
lm(duration ~ n_pages) %>%
summary()
##
## Call:
## lm(formula = duration ~ n_pages)
##
## Residuals:
## Min 1Q Median 3Q Max
## -386.45 -213.03 -38.93 179.31 602.55
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 404.803 11.323 35.750 < 2e-16 ***
## n_pages -8.355 1.296 -6.449 1.76e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 263.3 on 998 degrees of freedom
## Multiple R-squared: 0.04, Adjusted R-squared: 0.03904
22
String Manipulation
email <- 'jovialcann@anymail.com'
# without pipe
str_to_upper(str_sub(str_split(email, '@')[[1]][1], start = 1, end = 6))
## [1] "JOVIAL"
23
String Manipulation - Using Pipe
# with pipe
email %>%
str_split(pattern = '@') %>%
extract2(1) %>%
extract(1) %>%
str_sub(start = 1, end = 6) %>%
str_to_upper()
## [1] "JOVIAL"
24
Data Extraction
extract()
extract2()
use_series()
•
•
•
25
Extract Column By Name
ecom_mini['n_pages']
## # A tibble: 10 x 1
## n_pages
## <dbl>
## 1 5
## 2 17
## 3 18
## 4 5
## 5 16
## 6 6
## 7 13
## 8 10
## 9 14
## 10 1
extract(ecom_mini, 'n_pages')
## # A tibble: 10 x 1
## n_pages
## <dbl> 26
Extract Column By Position
ecom_mini[2]
## # A tibble: 10 x 1
## n_pages
## <dbl>
## 1 5
## 2 17
## 3 18
## 4 5
## 5 16
## 6 6
## 7 13
## 8 10
## 9 14
## 10 1
27
Extract Column By Position
extract(ecom_mini, 2)
## # A tibble: 10 x 1
## n_pages
## <dbl>
## 1 5
## 2 17
## 3 18
## 4 5
## 5 16
## 6 6
## 7 13
## 8 10
## 9 14
## 10 1
28
Extract Column (as vector)
ecom_mini$n_pages
## [1] 5 17 18 5 16 6 13 10 14 1
29
Extract Column (as vector)
use_series(ecom_mini, 'n_pages')
## [1] 5 17 18 5 16 6 13 10 14 1
30
Sample List
ecom_list <- as.list(ecom_mini)
31
Extract List Element By Name
# base
ecom_list[['n_pages']]
## [1] 5 17 18 5 16 6 13 10 14 1
ecom_list$n_pages
## [1] 5 17 18 5 16 6 13 10 14 1
32
Extract List Element By Name
# magrittr
extract2(ecom_list, 'n_pages')
## [1] 5 17 18 5 16 6 13 10 14 1
use_series(ecom_list, n_pages)
## [1] 5 17 18 5 16 6 13 10 14 1
33
Extract List Element By Position
# base
ecom_list[[1]]
## [1] direct social yahoo direct yahoo bing direct social direct g
## Levels: bing direct social yahoo google
# magrittr
extract2(ecom_list, 1)
## [1] direct social yahoo direct yahoo bing direct social direct g
## Levels: bing direct social yahoo google
34
Extract List Element (as vector)
# base
ecom_list$n_pages
## [1] 5 17 18 5 16 6 13 10 14 1
# magrittr
use_series(ecom_list, n_pages)
## [1] 5 17 18 5 16 6 13 10 14 1
35
Arithmetic Operations
add()
subtract()
multiply_by()
multiply_by_matrix()
divide_by()
divide_by_int()
mod()
raise_to_power()
•
•
•
•
•
•
•
•
36
Addition
1:10 + 1
## [1] 2 3 4 5 6 7 8 9 10 11
add(1:10, 1)
## [1] 2 3 4 5 6 7 8 9 10 11
`+`(1:10, 1)
## [1] 2 3 4 5 6 7 8 9 10 11
37
Multiplication
1:10 * 3
## [1] 3 6 9 12 15 18 21 24 27 30
multiply_by(1:10, 3)
## [1] 3 6 9 12 15 18 21 24 27 30
`*`(1:10, 3)
## [1] 3 6 9 12 15 18 21 24 27 30
38
Division
1:10 / 2
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
divide_by(1:10, 2)
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
`/`(1:10, 2)
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
39
Power
1:10 ^ 2
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
raise_to_power(1:10, 2)
## [1] 1 4 9 16 25 36 49 64 81 100
`^`(1:10, 2)
## [1] 1 4 9 16 25 36 49 64 81 100
40
Logical Operators
and()
or()
equals()
not()
is_greater_than()
is_weakly_greater_than()
is_less_than()
is_weakly_less_than()
•
•
•
•
•
•
•
•
41
Greater Than
1:10 > 5
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
is_greater_than(1:10, 5)
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
`>`(1:10, 5)
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
42
Weakly Greater Than
1:10 >= 5
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
is_weakly_greater_than(1:10, 5)
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
`>=`(1:10, 5)
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
43
44

More Related Content

What's hot

M12 random forest-part01
M12 random forest-part01M12 random forest-part01
M12 random forest-part01Raman Kannan
 
Data mining in Apriori and FP-tree
Data mining in Apriori and FP-treeData mining in Apriori and FP-tree
Data mining in Apriori and FP-treeSubhash Rohit
 
M11 bagging loo cv
M11 bagging loo cvM11 bagging loo cv
M11 bagging loo cvRaman Kannan
 
Seistech SQL code
Seistech SQL codeSeistech SQL code
Seistech SQL codeSimon Hoyle
 
Adventures on live partitioning
Adventures on live partitioningAdventures on live partitioning
Adventures on live partitioningMatteo Melli
 
M09-Cross validating-naive-bayes
M09-Cross validating-naive-bayesM09-Cross validating-naive-bayes
M09-Cross validating-naive-bayesRaman Kannan
 
Python data structures
Python data structuresPython data structures
Python data structuresHarry Potter
 
Easy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraEasy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraBarry DeCicco
 
Poodr ch8-composition
Poodr ch8-compositionPoodr ch8-composition
Poodr ch8-compositionorga shih
 
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPlotly
 
Bouncingballs sh
Bouncingballs shBouncingballs sh
Bouncingballs shBen Pope
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Traceoysteing
 
Functional UI and Unidirectional Dataflow
Functional UI and Unidirectional DataflowFunctional UI and Unidirectional Dataflow
Functional UI and Unidirectional Dataflowmikaelbr
 

What's hot (16)

M12 random forest-part01
M12 random forest-part01M12 random forest-part01
M12 random forest-part01
 
Data mining in Apriori and FP-tree
Data mining in Apriori and FP-treeData mining in Apriori and FP-tree
Data mining in Apriori and FP-tree
 
M11 bagging loo cv
M11 bagging loo cvM11 bagging loo cv
M11 bagging loo cv
 
Seistech SQL code
Seistech SQL codeSeistech SQL code
Seistech SQL code
 
Adventures on live partitioning
Adventures on live partitioningAdventures on live partitioning
Adventures on live partitioning
 
Computer practicals(part) Class 12
Computer practicals(part) Class 12Computer practicals(part) Class 12
Computer practicals(part) Class 12
 
M09-Cross validating-naive-bayes
M09-Cross validating-naive-bayesM09-Cross validating-naive-bayes
M09-Cross validating-naive-bayes
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Easy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtraEasy HTML Tables in RStudio with Tabyl and kableExtra
Easy HTML Tables in RStudio with Tabyl and kableExtra
 
Poodr ch8-composition
Poodr ch8-compositionPoodr ch8-composition
Poodr ch8-composition
 
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
 
Bouncingballs sh
Bouncingballs shBouncingballs sh
Bouncingballs sh
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
Functional UI and Unidirectional Dataflow
Functional UI and Unidirectional DataflowFunctional UI and Unidirectional Dataflow
Functional UI and Unidirectional Dataflow
 
Codigos
CodigosCodigos
Codigos
 
Quick reference for hql
Quick reference for hqlQuick reference for hql
Quick reference for hql
 

Similar to Writing Readable Code with Pipes

Data manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsyData manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsySmartHinJ
 
MH prediction modeling and validation in r (1) regression 190709
MH prediction modeling and validation in r (1) regression 190709MH prediction modeling and validation in r (1) regression 190709
MH prediction modeling and validation in r (1) regression 190709Min-hyung Kim
 
R - Practice.pptx
R - Practice.pptxR - Practice.pptx
R - Practice.pptxSowmyaMani8
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RStacy Irwin
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with RYanchang Zhao
 
Forecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsForecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsGeoffery Mullings
 
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)Wataru Shito
 
Descriptive analytics in r programming language
Descriptive analytics in r programming languageDescriptive analytics in r programming language
Descriptive analytics in r programming languageAshwini Mathur
 
R Matrix Math Quick Reference
R Matrix Math Quick ReferenceR Matrix Math Quick Reference
R Matrix Math Quick ReferenceMark Niemann-Ross
 
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docx
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docxmetadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docx
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docxARIV4
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RRsquared Academy
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdfzehiwot hone
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 
Model Selection and Multi-model Inference
Model Selection and Multi-model InferenceModel Selection and Multi-model Inference
Model Selection and Multi-model Inferencerichardchandler
 
第5回 様々なファイル形式の読み込みとデータの書き出し
第5回 様々なファイル形式の読み込みとデータの書き出し第5回 様々なファイル形式の読み込みとデータの書き出し
第5回 様々なファイル形式の読み込みとデータの書き出しWataru Shito
 

Similar to Writing Readable Code with Pipes (20)

Data manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsyData manipulation and visualization in r 20190711 myanmarucsy
Data manipulation and visualization in r 20190711 myanmarucsy
 
MH prediction modeling and validation in r (1) regression 190709
MH prediction modeling and validation in r (1) regression 190709MH prediction modeling and validation in r (1) regression 190709
MH prediction modeling and validation in r (1) regression 190709
 
R - Practice.pptx
R - Practice.pptxR - Practice.pptx
R - Practice.pptx
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with R
 
Forecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsForecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series Models
 
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
第5回 様々なファイル形式の読み込みとデータの書き出し(解答付き)
 
Descriptive analytics in r programming language
Descriptive analytics in r programming languageDescriptive analytics in r programming language
Descriptive analytics in r programming language
 
R Matrix Math Quick Reference
R Matrix Math Quick ReferenceR Matrix Math Quick Reference
R Matrix Math Quick Reference
 
R programming language
R programming languageR programming language
R programming language
 
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docx
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docxmetadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docx
metadatacoreProperties.xmlModel2015-07-13T030104Zthua3267th.docx
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In R
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
 
令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Dplyr and Plyr
Dplyr and PlyrDplyr and Plyr
Dplyr and Plyr
 
Model Selection and Multi-model Inference
Model Selection and Multi-model InferenceModel Selection and Multi-model Inference
Model Selection and Multi-model Inference
 
第5回 様々なファイル形式の読み込みとデータの書き出し
第5回 様々なファイル形式の読み込みとデータの書き出し第5回 様々なファイル形式の読み込みとデータの書き出し
第5回 様々なファイル形式の読み込みとデータの書き出し
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 

More from Rsquared Academy

Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in RRsquared Academy
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersRsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsRsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data TypesRsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsRsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsRsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersRsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsRsquared Academy
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeRsquared Academy
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: IntroductionRsquared Academy
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RRsquared Academy
 

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
 

Recently uploaded

Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
Kings of Saudi Arabia, information about them
Kings of Saudi Arabia, information about themKings of Saudi Arabia, information about them
Kings of Saudi Arabia, information about themeitharjee
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...HyderabadDolls
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...gajnagarg
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...Elaine Werffeli
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...gajnagarg
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...HyderabadDolls
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Klinik kandungan
 
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...HyderabadDolls
 
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...HyderabadDolls
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...kumargunjan9515
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabiaahmedjiabur940
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxronsairoathenadugay
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...nirzagarg
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制vexqp
 

Recently uploaded (20)

Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
 
Kings of Saudi Arabia, information about them
Kings of Saudi Arabia, information about themKings of Saudi Arabia, information about them
Kings of Saudi Arabia, information about them
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...
Jodhpur Park | Call Girls in Kolkata Phone No 8005736733 Elite Escort Service...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
 
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 

Writing Readable Code with Pipes

  • 1. 1
  • 2. Agenda what are pipes? why use pipes? what are the different types of pipes? combining operations with pipes case studies • • • • • 2
  • 3. Introduction R code contain a lot of parentheses in case of a sequence of multiple operations. When you are dealing with complex code, it results in nested function calls which are hard to read and maintain. The package by provides pipes enabling us to write R code that is readable. Pipes allow us to clearly express a sequence of multiple operations by: structuring operations from left to right avoiding nested function calls intermediate steps overwriting of original data minimizing creation of local variables magrittr Stefan Milton Bache • • • • • • 3
  • 4. Pipes If you are using , magrittr will be automatically loaded. We will look at 3 different types of pipes: %>% : pipe a value forward into an expression or function call %<>%: result assigned to left hand side object instead of returning it %$% : expose names within left hand side objects to right hand side expressions tidyverse • • • 4
  • 6. Data ## # A tibble: 1,000 x 4 ## referrer n_pages duration purchase ## <fct> <dbl> <dbl> <lgl> ## 1 google 1 693 FALSE ## 2 yahoo 1 459 FALSE ## 3 direct 1 996 FALSE ## 4 bing 18 468 TRUE ## 5 yahoo 1 955 FALSE ## 6 yahoo 5 135 FALSE ## 7 yahoo 1 75 FALSE ## 8 direct 1 908 FALSE ## 9 bing 19 209 FALSE ## 10 google 1 208 FALSE ## # ... with 990 more rows 6
  • 7. Data Dictionary referrer: referrer website/search engine n_pages: number of pages visited duration: time spent on the website (in seconds) purchase: whether visitor purchased • • • • 7
  • 8. Sample Data ecom_mini <- sample_n(ecom, size = 10) 8
  • 9. head head(ecom, 10) ## # A tibble: 10 x 4 ## referrer n_pages duration purchase ## <fct> <dbl> <dbl> <lgl> ## 1 google 1 693 FALSE ## 2 yahoo 1 459 FALSE ## 3 direct 1 996 FALSE ## 4 bing 18 468 TRUE ## 5 yahoo 1 955 FALSE ## 6 yahoo 5 135 FALSE ## 7 yahoo 1 75 FALSE ## 8 direct 1 908 FALSE ## 9 bing 19 209 FALSE ## 10 google 1 208 FALSE 9
  • 10. Using pipe ecom %>% head(10) ## # A tibble: 10 x 4 ## referrer n_pages duration purchase ## <fct> <dbl> <dbl> <lgl> ## 1 google 1 693 FALSE ## 2 yahoo 1 459 FALSE ## 3 direct 1 996 FALSE ## 4 bing 18 468 TRUE ## 5 yahoo 1 955 FALSE ## 6 yahoo 5 135 FALSE ## 7 yahoo 1 75 FALSE ## 8 direct 1 908 FALSE ## 9 bing 19 209 FALSE ## 10 google 1 208 FALSE 10
  • 11. Square Root y <- ecom_mini$n_pages y <- sqrt(y) # combine above steps sqrt(ecom_mini$n_pages) ## [1] 2.236068 4.123106 4.242641 2.236068 4.000000 2.449490 3.605551 ## [8] 3.162278 3.741657 1.000000 11
  • 12. Square Root - Using pipe # select n_pages variable and assign it to y ecom_mini %$% n_pages -> y # compute square root of y and assign it to y y %<>% sqrt() 12
  • 13. 13
  • 14. Square Root - Using pipe ecom_mini %$% n_pages %>% sqrt() -> y y ## [1] 2.236068 4.123106 4.242641 2.236068 4.000000 2.449490 3.605551 ## [8] 3.162278 3.741657 1.000000 14
  • 15. 15
  • 16. Correlation # without pipe ecom1 <- subset(ecom, purchase) cor(ecom1$n_pages, ecom1$duration) ## [1] 0.4290905 16
  • 17. Correlation - Using pipe # with pipe ecom %>% subset(purchase) %$% cor(n_pages, duration) ## [1] 0.4290905 # using filter from dplyr and pipe ecom %>% filter(purchase) %$% cor(n_pages, duration) ## [1] 0.4290905 17
  • 19. 19
  • 20. Visualization - Using pipe ecom %>% subset(purchase) %>% extract('referrer') %>% table() %>% barplot() 20
  • 21. Regression summary(lm(duration ~ n_pages, data = ecom)) ## ## Call: ## lm(formula = duration ~ n_pages, data = ecom) ## ## Residuals: ## Min 1Q Median 3Q Max ## -386.45 -213.03 -38.93 179.31 602.55 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 404.803 11.323 35.750 < 2e-16 *** ## n_pages -8.355 1.296 -6.449 1.76e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 263.3 on 998 degrees of freedom ## Multiple R-squared: 0.04, Adjusted R-squared: 0.03904 ## F-statistic: 41 58 on 1 and 998 DF p-value: 1 756e-10 21
  • 22. Regression - Using pipe ecom %$% lm(duration ~ n_pages) %>% summary() ## ## Call: ## lm(formula = duration ~ n_pages) ## ## Residuals: ## Min 1Q Median 3Q Max ## -386.45 -213.03 -38.93 179.31 602.55 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 404.803 11.323 35.750 < 2e-16 *** ## n_pages -8.355 1.296 -6.449 1.76e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 263.3 on 998 degrees of freedom ## Multiple R-squared: 0.04, Adjusted R-squared: 0.03904 22
  • 23. String Manipulation email <- 'jovialcann@anymail.com' # without pipe str_to_upper(str_sub(str_split(email, '@')[[1]][1], start = 1, end = 6)) ## [1] "JOVIAL" 23
  • 24. String Manipulation - Using Pipe # with pipe email %>% str_split(pattern = '@') %>% extract2(1) %>% extract(1) %>% str_sub(start = 1, end = 6) %>% str_to_upper() ## [1] "JOVIAL" 24
  • 26. Extract Column By Name ecom_mini['n_pages'] ## # A tibble: 10 x 1 ## n_pages ## <dbl> ## 1 5 ## 2 17 ## 3 18 ## 4 5 ## 5 16 ## 6 6 ## 7 13 ## 8 10 ## 9 14 ## 10 1 extract(ecom_mini, 'n_pages') ## # A tibble: 10 x 1 ## n_pages ## <dbl> 26
  • 27. Extract Column By Position ecom_mini[2] ## # A tibble: 10 x 1 ## n_pages ## <dbl> ## 1 5 ## 2 17 ## 3 18 ## 4 5 ## 5 16 ## 6 6 ## 7 13 ## 8 10 ## 9 14 ## 10 1 27
  • 28. Extract Column By Position extract(ecom_mini, 2) ## # A tibble: 10 x 1 ## n_pages ## <dbl> ## 1 5 ## 2 17 ## 3 18 ## 4 5 ## 5 16 ## 6 6 ## 7 13 ## 8 10 ## 9 14 ## 10 1 28
  • 29. Extract Column (as vector) ecom_mini$n_pages ## [1] 5 17 18 5 16 6 13 10 14 1 29
  • 30. Extract Column (as vector) use_series(ecom_mini, 'n_pages') ## [1] 5 17 18 5 16 6 13 10 14 1 30
  • 31. Sample List ecom_list <- as.list(ecom_mini) 31
  • 32. Extract List Element By Name # base ecom_list[['n_pages']] ## [1] 5 17 18 5 16 6 13 10 14 1 ecom_list$n_pages ## [1] 5 17 18 5 16 6 13 10 14 1 32
  • 33. Extract List Element By Name # magrittr extract2(ecom_list, 'n_pages') ## [1] 5 17 18 5 16 6 13 10 14 1 use_series(ecom_list, n_pages) ## [1] 5 17 18 5 16 6 13 10 14 1 33
  • 34. Extract List Element By Position # base ecom_list[[1]] ## [1] direct social yahoo direct yahoo bing direct social direct g ## Levels: bing direct social yahoo google # magrittr extract2(ecom_list, 1) ## [1] direct social yahoo direct yahoo bing direct social direct g ## Levels: bing direct social yahoo google 34
  • 35. Extract List Element (as vector) # base ecom_list$n_pages ## [1] 5 17 18 5 16 6 13 10 14 1 # magrittr use_series(ecom_list, n_pages) ## [1] 5 17 18 5 16 6 13 10 14 1 35
  • 37. Addition 1:10 + 1 ## [1] 2 3 4 5 6 7 8 9 10 11 add(1:10, 1) ## [1] 2 3 4 5 6 7 8 9 10 11 `+`(1:10, 1) ## [1] 2 3 4 5 6 7 8 9 10 11 37
  • 38. Multiplication 1:10 * 3 ## [1] 3 6 9 12 15 18 21 24 27 30 multiply_by(1:10, 3) ## [1] 3 6 9 12 15 18 21 24 27 30 `*`(1:10, 3) ## [1] 3 6 9 12 15 18 21 24 27 30 38
  • 39. Division 1:10 / 2 ## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 divide_by(1:10, 2) ## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 `/`(1:10, 2) ## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 39
  • 40. Power 1:10 ^ 2 ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 ## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 ## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 raise_to_power(1:10, 2) ## [1] 1 4 9 16 25 36 49 64 81 100 `^`(1:10, 2) ## [1] 1 4 9 16 25 36 49 64 81 100 40
  • 42. Greater Than 1:10 > 5 ## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE is_greater_than(1:10, 5) ## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE `>`(1:10, 5) ## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE 42
  • 43. Weakly Greater Than 1:10 >= 5 ## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE is_weakly_greater_than(1:10, 5) ## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE `>=`(1:10, 5) ## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE 43
  • 44. 44