SlideShare a Scribd company logo
パイ プ入門
Introdunction to pipe
NAKAJIMA Yukihiro
2018/1/16
Y.NAKAJIMA パイ プ入門 2018/1/16 1 / 17
1 はじ めに
2 %>%
3 %<>%
4 %$%
Y.NAKAJIMA パイ プ入門 2018/1/16 2 / 17
はじ めに
Y.NAKAJIMA パイ プ入門 2018/1/16 3 / 17
データ の前処理などをする と き にいく つも 変数があっ て面倒く さ い
メ モリ も どんどん消費し てし ま う
ネスト さ れたコード は読みづら い
それら を解決する 手段と し てパイ プ ( %>% etc.) があり ま す。
Y.NAKAJIMA パイ プ入門 2018/1/16 4 / 17
パイ プと は
パイ プ演算子 ( %>% ) を使う こ と で、 パイ プ演算子ま での処理をパ
イ プ演算子の後の関数の第一引数に入れる こ と ができ る
magrittr パッ ケージの中の関数の 1 つ
dplyr を読み込むと 一緒に読み込んでく れる
ctrl(cmd) + Shift(opt) + M で書く こ と ができ ま す。
詳し く は magrittr の vignettes などを見てく ださ い。
Y.NAKAJIMA パイ プ入門 2018/1/16 5 / 17
%>%
Y.NAKAJIMA パイ プ入門 2018/1/16 6 / 17
と り あえずやっ てみま し ょ う
library(magrittr)
library(dplyr)
iris %>%
select(1:3) %>%
filter(Sepal.Length <= 5)
Sepal.Length Sepal.Width Petal.Length
1 4.90 3.00 1.40
2 4.70 3.20 1.30
3 4.60 3.10 1.50
4 5.00 3.60 1.40
5 4.60 3.40 1.40
6 5.00 3.40 1.50
Y.NAKAJIMA パイ プ入門 2018/1/16 7 / 17
第一引数以外で利用し たい場合は. を利用し ま す。
次のスライ ド の lm() の場合、 formula の. と data の. は意味が違う こ と に
注意し ま し ょ う 。
前者は説明変数をすべて投入し 、 後者はパイ プの前ま での処理を引き 渡
し ま す。
Y.NAKAJIMA パイ プ入門 2018/1/16 8 / 17
iris %>%
lm(Sepal.Length ~ ., data = .) %>%
summary()
Call:
lm(formula = Sepal.Length ~ ., data = .)
Residuals:
Min 1Q Median 3Q Max
-0.79424 -0.21874 0.00899 0.20255 0.73103
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.17127 0.27979 7.760 1.43e-12 ***
Sepal.Width 0.49589 0.08607 5.761 4.87e-08 ***
Petal.Length 0.82924 0.06853 12.101 < 2e-16 ***
Petal.Width -0.31516 0.15120 -2.084 0.03889 *
Speciesversicolor -0.72356 0.24017 -3.013 0.00306 **
Speciesvirginica -1.02350 0.33373 -3.067 0.00258 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Y.NAKAJIMA パイ プ入門 2018/1/16 9 / 17
本旨と は関係ないですが、 stargazer パッ ケージを使う と き れいに回帰分
析の結果を表示でき ま す。
library(stargazer)
iris %>%
lm(Sepal.Length ~ Sepal.Width, data = .) %>%
stargazer(type = ”latex”, header=FALSE, float=FALSE,
ci=TRUE, ci.level=0.90, single.row=TRUE)
Y.NAKAJIMA パイ プ入門 2018/1/16 10 / 17
Dependent variable:
Sepal.Length
Sepal.Width −0.223 (−0.478, 0.032)
Constant 6.526∗∗∗ (5.739, 7.314)
Observations 150
R2 0.014
Adjusted R2 0.007
Residual Std. Error 0.825 (df = 148)
F Statistic 2.074 (df = 1; 148)
Note: ∗p<0.1; ∗∗p<0.05; ∗∗∗p<0.01
Y.NAKAJIMA パイ プ入門 2018/1/16 11 / 17
%<>%
Y.NAKAJIMA パイ プ入門 2018/1/16 12 / 17
関数の連結によ っ て処理し た結果を元の変数に代入し なおし て結果を
アッ プデート し たい場合には%<>%を使いま す。
iris %<>%
filter(Sepal.Length <= 5) %>%
head(3)
iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
Y.NAKAJIMA パイ プ入門 2018/1/16 13 / 17
%$%
Y.NAKAJIMA パイ プ入門 2018/1/16 14 / 17
関数を連結する 際に、 %>%では対応でき ないこ と も あり ま す。
例えば、 変数にデータ セッ ト を格納し ている が、 関数ではデータ セッ ト
ではなく 、 列に格納さ れた数値だけを使いたい場合などです。 具体的に
は相関係数の算出などが挙げら れま す。
iris %>%
filter(Sepal.Length <= 5) %$%
cor.test(Sepal.Length,
Sepal.Width)
Pearson's product-
moment correlation
data: Sepal.Length and Sepal.Width
t = -0.86603, df = 1, p-
value = 0.5456
alternative hypothesis: true correlation
sample estimates:
cor
-0.6546537
Y.NAKAJIMA パイ プ入門 2018/1/16 15 / 17
2 つ以上の処理を同時に行いたい場合には{}で囲みま す。
iris %>% filter(Sepal.Length <= 5) %$% {
plot(Sepal.Length, Petal.Width)
mean(Sepal.Length)
}
4.60 4.65 4.70 4.75 4.80 4.85 4.90
0.150.200.25
Sepal.Length
Petal.Width
[1] 4.733333
Y.NAKAJIMA パイ プ入門 2018/1/16 16 / 17
今回紹介し た magrittr パッ ケージのほかにも pipeR パッ ケージがあり
ま す。
こ ちら を使う と さ ら に柔軟に R が書ける よ う になる と 思いま す。
Enjoy!
Y.NAKAJIMA パイ プ入門 2018/1/16 17 / 17

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

パイプ入門 Introduction to pipe

  • 1. パイ プ入門 Introdunction to pipe NAKAJIMA Yukihiro 2018/1/16 Y.NAKAJIMA パイ プ入門 2018/1/16 1 / 17
  • 2. 1 はじ めに 2 %>% 3 %<>% 4 %$% Y.NAKAJIMA パイ プ入門 2018/1/16 2 / 17
  • 3. はじ めに Y.NAKAJIMA パイ プ入門 2018/1/16 3 / 17
  • 4. データ の前処理などをする と き にいく つも 変数があっ て面倒く さ い メ モリ も どんどん消費し てし ま う ネスト さ れたコード は読みづら い それら を解決する 手段と し てパイ プ ( %>% etc.) があり ま す。 Y.NAKAJIMA パイ プ入門 2018/1/16 4 / 17
  • 5. パイ プと は パイ プ演算子 ( %>% ) を使う こ と で、 パイ プ演算子ま での処理をパ イ プ演算子の後の関数の第一引数に入れる こ と ができ る magrittr パッ ケージの中の関数の 1 つ dplyr を読み込むと 一緒に読み込んでく れる ctrl(cmd) + Shift(opt) + M で書く こ と ができ ま す。 詳し く は magrittr の vignettes などを見てく ださ い。 Y.NAKAJIMA パイ プ入門 2018/1/16 5 / 17
  • 7. と り あえずやっ てみま し ょ う library(magrittr) library(dplyr) iris %>% select(1:3) %>% filter(Sepal.Length <= 5) Sepal.Length Sepal.Width Petal.Length 1 4.90 3.00 1.40 2 4.70 3.20 1.30 3 4.60 3.10 1.50 4 5.00 3.60 1.40 5 4.60 3.40 1.40 6 5.00 3.40 1.50 Y.NAKAJIMA パイ プ入門 2018/1/16 7 / 17
  • 8. 第一引数以外で利用し たい場合は. を利用し ま す。 次のスライ ド の lm() の場合、 formula の. と data の. は意味が違う こ と に 注意し ま し ょ う 。 前者は説明変数をすべて投入し 、 後者はパイ プの前ま での処理を引き 渡 し ま す。 Y.NAKAJIMA パイ プ入門 2018/1/16 8 / 17
  • 9. iris %>% lm(Sepal.Length ~ ., data = .) %>% summary() Call: lm(formula = Sepal.Length ~ ., data = .) Residuals: Min 1Q Median 3Q Max -0.79424 -0.21874 0.00899 0.20255 0.73103 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 2.17127 0.27979 7.760 1.43e-12 *** Sepal.Width 0.49589 0.08607 5.761 4.87e-08 *** Petal.Length 0.82924 0.06853 12.101 < 2e-16 *** Petal.Width -0.31516 0.15120 -2.084 0.03889 * Speciesversicolor -0.72356 0.24017 -3.013 0.00306 ** Speciesvirginica -1.02350 0.33373 -3.067 0.00258 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Y.NAKAJIMA パイ プ入門 2018/1/16 9 / 17
  • 10. 本旨と は関係ないですが、 stargazer パッ ケージを使う と き れいに回帰分 析の結果を表示でき ま す。 library(stargazer) iris %>% lm(Sepal.Length ~ Sepal.Width, data = .) %>% stargazer(type = ”latex”, header=FALSE, float=FALSE, ci=TRUE, ci.level=0.90, single.row=TRUE) Y.NAKAJIMA パイ プ入門 2018/1/16 10 / 17
  • 11. Dependent variable: Sepal.Length Sepal.Width −0.223 (−0.478, 0.032) Constant 6.526∗∗∗ (5.739, 7.314) Observations 150 R2 0.014 Adjusted R2 0.007 Residual Std. Error 0.825 (df = 148) F Statistic 2.074 (df = 1; 148) Note: ∗p<0.1; ∗∗p<0.05; ∗∗∗p<0.01 Y.NAKAJIMA パイ プ入門 2018/1/16 11 / 17
  • 12. %<>% Y.NAKAJIMA パイ プ入門 2018/1/16 12 / 17
  • 13. 関数の連結によ っ て処理し た結果を元の変数に代入し なおし て結果を アッ プデート し たい場合には%<>%を使いま す。 iris %<>% filter(Sepal.Length <= 5) %>% head(3) iris Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa Y.NAKAJIMA パイ プ入門 2018/1/16 13 / 17
  • 14. %$% Y.NAKAJIMA パイ プ入門 2018/1/16 14 / 17
  • 15. 関数を連結する 際に、 %>%では対応でき ないこ と も あり ま す。 例えば、 変数にデータ セッ ト を格納し ている が、 関数ではデータ セッ ト ではなく 、 列に格納さ れた数値だけを使いたい場合などです。 具体的に は相関係数の算出などが挙げら れま す。 iris %>% filter(Sepal.Length <= 5) %$% cor.test(Sepal.Length, Sepal.Width) Pearson's product- moment correlation data: Sepal.Length and Sepal.Width t = -0.86603, df = 1, p- value = 0.5456 alternative hypothesis: true correlation sample estimates: cor -0.6546537 Y.NAKAJIMA パイ プ入門 2018/1/16 15 / 17
  • 16. 2 つ以上の処理を同時に行いたい場合には{}で囲みま す。 iris %>% filter(Sepal.Length <= 5) %$% { plot(Sepal.Length, Petal.Width) mean(Sepal.Length) } 4.60 4.65 4.70 4.75 4.80 4.85 4.90 0.150.200.25 Sepal.Length Petal.Width [1] 4.733333 Y.NAKAJIMA パイ プ入門 2018/1/16 16 / 17
  • 17. 今回紹介し た magrittr パッ ケージのほかにも pipeR パッ ケージがあり ま す。 こ ちら を使う と さ ら に柔軟に R が書ける よ う になる と 思いま す。 Enjoy! Y.NAKAJIMA パイ プ入門 2018/1/16 17 / 17