SlideShare a Scribd company logo
1 of 27
Rの基礎いろいろ
2013.10.13
TOKYO.R JOSHIBU #2
Rとわたし
• はじめてRに触れたのは、大学のときの授業。
• 仕事でR使用中。主にデータ処理やグラフ作成など。
• Rを使ってモデリングとかシュミレーションしたい。
(今後仕事で必要になるので…)
• 現在、ちょっとずつ統計を勉強中。
今日やること
前置きが長くなってすいません…
(本当は)CodeIQ に出題されてた
Rの問題をやる予定でした…。。
URL: https://codeiq.jp/
ただ、〆切が結構先なので
別の内容にしました。
irisデータを
使ってプロットす
る
irisデータとは…
• Rには100以上のデータセットが付属。
• iris データは、3品種のあやめのがく片や花びらの長さや幅
などのデータから成り立っている。
• Fisher の研究でも使われたデータ。
• Rのデータセットについて詳しく説明されているサイト様。
http://d.hatena.ne.jp/hoxo_m/20120214/p1
iris データをみる
• コマンド実行するとデータが一気に表示されるはず。
• このデータの内容、もうちょっと簡単にみたいんだけど?
• そんなときは、str() を使う。
$ iris
str()でデータの内容をみる
$ str(iris)
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1
1 1 1 1 1 ...
データ構造
• もうちょっと詳しくみたい、そんなときはsummary() !
行数、列数
データの特徴
summary() で特徴(要約等計量)を
みる
$ summary(iris)
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50
Min.: 最小値
1st Qu.:第一四分位数
Median: 中央値
Mean: 平均値
3rd Qu.: 第三四分位数
Max.: 最大値
これはFactor型なので
品種ごとの行数が表
示
データにアクセス
• 部分データを取得したい場合には、c関数を使う
$ iris[1:30, 1:2]
行アクセス 列アクセス
$ iris$Sepal.Length
データフレーム$変数
名
変数名を取得する
$ colnames(iris)
$ rownames(iris)
行名取得
列名取得
とりあえず、plot()
$ plot(iris[,1], iris[,2])
X軸, Y軸のラベルを変更
$ plot(iris[,1], iris[,2], xlab=“Sepal.Length”,
ylab=“Sepal.Width”)
色をつける
$ plot(iris[,1], iris[,2], xlab=“Sepal.Length”, ylab=“Sepal.Wi
dth”, col=“blue”)
軸の長さを指定する
$ plot(iris[,1], iris[,2], xlab=“Sepal.Length”,
ylab=“Sepal.Width”, col=“blue”, xlim=c(0,8), ylim=c(0,8))
プロットマーカー変更
$ plot(iris[,1], iris[,2], xlab=“Sepal.Length”, ylab=“Sepal.Width”,
col=“blue”, xlim=c(0,8), ylim=c(0,8), pch=20)
プロットマーカー一覧
• pch=値 で点をプロットする文字を指定できる
品種ごとにマーカーの色を変更
$ plot(iris$Sepal.Length, iris$Sepal.Width,
xlab="Sepal.Length", ylab="Sepal.Width",
col=c(1:length(unique(iris$Species))), ylim=c(2,6),
pch=20)
3でもいい
品種ごとにマーカーの色を変更
凡例を追加する
$ plot(iris$Sepal.Length, iris$Sepal.Width,
xlab="Sepal.Length", ylab="Sepal.Width",
col=c(1:length(unique(iris$Species))), ylim=c(2,6),
pch=20)
$ legend("topright", legend=unique(iris$Species),
col=c(1:length(unique(iris$Species))),pch=20)
凡例を追加する
グラフに文字を書き込む
• そのまえに、Sepal.Length とSepal.Width の相関係数を求め
る
(書き込むデータを用意するため)
• text() を使って求めた相関係数をグラフに書き込む
$ r <- cor(iris$Sepal.Length, iris$Sepal.Width)
$ r
[1] -0.1175698
グラフに文字を書き込む
$ plot(iris$Sepal.Length, iris$Sepal.Width, xlab="Sepal.Le
ngth", ylab="Sepal.Width", col=“blue”, ylim=c(2,6), pch=2
0)
$ text(7, 5, label=r, srt=0, cex=0.8)
グラフに文字を書き込む
plot() ではものたりない場合…
• ggplot()
• パッケージをインストールする必要がある
$ install.packages(ggplot2)
$ library(ggplot2)
• 書き方になれないとかなり使いにくい…
ggplot() で描写してみるとこんな感
じ
$ ggplot(iris[,1:2], aes(Sepal.Length,
Sepal.Width))+geom_point()
• こんなかんじで描写
• 場合によっては、ggplot()
使ったほうが楽に描写で
きるものもある
(例:積み上げグラフと
か)
わからないときは…
• R−tips
http://cse.naro.affrc.go.jp/takezawa/r-tips/r.html
• RjpWiki
http://www.okada.jp.org/Rwiki/
• seekR
http://seekr.jp/
• ひたすらググる、もしくは人に聞く
ご清聴ありがとうございま
した
至らない点が多々あったと思いますが、
最後までお付き合いしていただきありがとうございました。
今後もどうぞよろしくお願いします。
E-mail: mf.0833 <at> gmail.com

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil 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 2024Albert 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 InsightsKurio // 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 2024Search 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 summarySpeakerHub
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit 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 managementMindGenius
 
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
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
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...
 

Tokyo.r 女子資料