SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
第2回Linked Open Dataハッカソン関西 in 大阪

オープンデータ と

R

チュートリアル
Rって
簡単!

かわはら
R で オープンデータ
• 鯖江市のデータを取得して
Webブラウザ上にマッピングする
R は 便利な解析ツール

統計
分析

• オープン & フリー
• グラフィックス
• 充実した解析環境
RStudio で 更に便利に
R用のIDE

統計
分析
R の 基本操作
•
•
•
•

(変数の代入)

a <- 1 #aに1を代入
b <- c(1:3) #bにベクトル(1,2,3)を代入
c <- c(‘x’, ‘y’, ‘z’)
d <- data.frame(d1=b, d2=c)
R の 基本操作

(データフレーム)

• d <- data.frame(d1=b, d2=c)
要素は「$」で表す
d1

d2

1

1

x

2

2

y

3

3

z

• d$d2[1] = d[1, 2] = “x”
R の 基本操作 (データの確認・準備)
• str(), head(), summary()でデータ確認
str(d$d1)
• as.XXXで型を変更 as.integer, as.character…
d$d1 <- as.character(d$d1)
• names()で要素の名前取得/命名
names(d)
names(d) <- c(“名前1”, “名前2”)
R の 基本操作 (パッケージの利用)
• Rの機能を拡張するためのパッケージ
• install.packages() … インストール
• library() or require() … 呼び出し
• XML … XMLファイル操作
• googleVis … Google Visualization API
R で オープンデータ
• 鯖江市のAED設置場所をマッピング
R で オープンデータ (コード)
• データ取得から可視化まで、これだけ
install.packages('XML'); library(XML) #XML package
install.packages('googleVis'); library(googleVis) #googleVis package
int <- "integer"; num <- "numeric"; chr <- "character" #データ型の略記
url <- "http://www3.city.sabae.fukui.jp/xml/aed/aed.xml" #鯖江市URL
data <- xmlToDataFrame(url, stringsAsFactors=F,
colClasses=c(int, chr, chr, int, num, num, chr)) #データ取得
data$latlong <- paste(data$latitude, data$longitude, sep=":") #データ加工
plot(gvisMap(data, locationvar="latlong", tipvar="name")) #表示
R で オープンデータ (データを取得)
• XMLデータを取得する
#XMLパッケージの導入
#この中の関数xmlToDataFrameを利用する
Install.packages(‘XML’)
library(XML)
#関数xmlToDataFrameを使ってXMLデータを取得
#この関数についてのヘルプを見るには、?xmlToDataFrame
int <- "integer“; num <- "numeric“; chr <- "character“
url <- "http://www3.city.sabae.fukui.jp/xml/aed/aed.xml"
data <- xmlToDataFrame(url,
stringsAsFactors=F, colClasses=c(int, chr, chr, int, num, num, chr))
R で オープンデータ (データの確認)
• データの中身を(簡単に)確認する
str(data)
'data.frame': 134 obs. of 7 variables:
$ no : int 1 115 116 117 118 119 120 121 122 123 ...
$ name : chr "鯖江高等学校 " "鯖江市嚮陽会館" "鯖江市役所" "JAたんなん ふれあいセンター " ...
$ address : chr "鯖江市舟津町2丁目5-42 " "鯖江市桜町2丁目7番1号" "鯖江市西山町13番1号"...
$ count : int 1 1 1 1 1 1 1 1 1 1 ...
$ latitude : num 35.9 35.9 36 36 36 ...
$ longitude: num 136 136 136 136 136 ...
$ url : chr "http://www3.city.sabae.fukui.jp/xml/aed/#1"
"http://www3.city.sabae.fukui.jp/xml/aed/#115” ...

latitude … 緯度
longitude … 経度
R で オープンデータ (データを加工)
• データをマッピング用に加工する
#googleVisパッケージの導入
#この中の関数gvisMapを利用する
Install.packages(‘googleVis’)
library(googleVis)
#gvisMap関数で座標を指定するには、「緯度:経度」の形式が必要
#dataに緯度:経度を要素とする項目latlongを追加する
#「データフレーム$項目名」<- 要素 で割り当て
#欲しい要素は「data$latitude:data$longitude」なので
#「data$latitude」と「data$longitude」を「:」で結合(paste)
data$latlong <- paste(data$latitude, data$longitude, sep=":")
R で オープンデータ (データを表示)
• マッピングする
#gvisMapを利用してマッピング用データを準備
map <- gvisMap(data,
locationvar = “latlong”,
tipvar = “name”,
options = list(showTip = F,
enableScrollWheel = T,
useMapTypeControl = T))
#作成したデータをブラウザで表示する
plot(map)
R で オープンデータ (完成?)
R で オープンデータ (SPARQLもできます!)
#SPARQLパッケージを導入する
install.pacages(‘SPARQL’)
library(SPARQL)
#エンドポイントの指定
endpoint <- http://services.data.gov/sparql
#クエリ
query <-"PREFIX dgp1187: <http://data-gov.tw.rpi.edu/vocab/p/1187/>
SELECT ?ye ?fi ?ac
WHERE {
?s dgp1187:year ?ye .
?s dgp1187:fires ?fi .
?s dgp1187:acres ?ac .
}"
#クエリ
qd <- SPARQL(endpoint,query); qd$result

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Kürzlich hochgeladen (11)

Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 

Empfohlen

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
ThinkNow
 
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)
 

Empfohlen (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...
 

オープンデータとR 第2回Linked Open Dataハッカソン関西 in 大阪

  • 1. 第2回Linked Open Dataハッカソン関西 in 大阪 オープンデータ と R チュートリアル Rって 簡単! かわはら
  • 2. R で オープンデータ • 鯖江市のデータを取得して Webブラウザ上にマッピングする
  • 3. R は 便利な解析ツール 統計 分析 • オープン & フリー • グラフィックス • 充実した解析環境
  • 5. R の 基本操作 • • • • (変数の代入) a <- 1 #aに1を代入 b <- c(1:3) #bにベクトル(1,2,3)を代入 c <- c(‘x’, ‘y’, ‘z’) d <- data.frame(d1=b, d2=c)
  • 6. R の 基本操作 (データフレーム) • d <- data.frame(d1=b, d2=c) 要素は「$」で表す d1 d2 1 1 x 2 2 y 3 3 z • d$d2[1] = d[1, 2] = “x”
  • 7. R の 基本操作 (データの確認・準備) • str(), head(), summary()でデータ確認 str(d$d1) • as.XXXで型を変更 as.integer, as.character… d$d1 <- as.character(d$d1) • names()で要素の名前取得/命名 names(d) names(d) <- c(“名前1”, “名前2”)
  • 8. R の 基本操作 (パッケージの利用) • Rの機能を拡張するためのパッケージ • install.packages() … インストール • library() or require() … 呼び出し • XML … XMLファイル操作 • googleVis … Google Visualization API
  • 9. R で オープンデータ • 鯖江市のAED設置場所をマッピング
  • 10. R で オープンデータ (コード) • データ取得から可視化まで、これだけ install.packages('XML'); library(XML) #XML package install.packages('googleVis'); library(googleVis) #googleVis package int <- "integer"; num <- "numeric"; chr <- "character" #データ型の略記 url <- "http://www3.city.sabae.fukui.jp/xml/aed/aed.xml" #鯖江市URL data <- xmlToDataFrame(url, stringsAsFactors=F, colClasses=c(int, chr, chr, int, num, num, chr)) #データ取得 data$latlong <- paste(data$latitude, data$longitude, sep=":") #データ加工 plot(gvisMap(data, locationvar="latlong", tipvar="name")) #表示
  • 11. R で オープンデータ (データを取得) • XMLデータを取得する #XMLパッケージの導入 #この中の関数xmlToDataFrameを利用する Install.packages(‘XML’) library(XML) #関数xmlToDataFrameを使ってXMLデータを取得 #この関数についてのヘルプを見るには、?xmlToDataFrame int <- "integer“; num <- "numeric“; chr <- "character“ url <- "http://www3.city.sabae.fukui.jp/xml/aed/aed.xml" data <- xmlToDataFrame(url, stringsAsFactors=F, colClasses=c(int, chr, chr, int, num, num, chr))
  • 12. R で オープンデータ (データの確認) • データの中身を(簡単に)確認する str(data) 'data.frame': 134 obs. of 7 variables: $ no : int 1 115 116 117 118 119 120 121 122 123 ... $ name : chr "鯖江高等学校 " "鯖江市嚮陽会館" "鯖江市役所" "JAたんなん ふれあいセンター " ... $ address : chr "鯖江市舟津町2丁目5-42 " "鯖江市桜町2丁目7番1号" "鯖江市西山町13番1号"... $ count : int 1 1 1 1 1 1 1 1 1 1 ... $ latitude : num 35.9 35.9 36 36 36 ... $ longitude: num 136 136 136 136 136 ... $ url : chr "http://www3.city.sabae.fukui.jp/xml/aed/#1" "http://www3.city.sabae.fukui.jp/xml/aed/#115” ... latitude … 緯度 longitude … 経度
  • 13. R で オープンデータ (データを加工) • データをマッピング用に加工する #googleVisパッケージの導入 #この中の関数gvisMapを利用する Install.packages(‘googleVis’) library(googleVis) #gvisMap関数で座標を指定するには、「緯度:経度」の形式が必要 #dataに緯度:経度を要素とする項目latlongを追加する #「データフレーム$項目名」<- 要素 で割り当て #欲しい要素は「data$latitude:data$longitude」なので #「data$latitude」と「data$longitude」を「:」で結合(paste) data$latlong <- paste(data$latitude, data$longitude, sep=":")
  • 14. R で オープンデータ (データを表示) • マッピングする #gvisMapを利用してマッピング用データを準備 map <- gvisMap(data, locationvar = “latlong”, tipvar = “name”, options = list(showTip = F, enableScrollWheel = T, useMapTypeControl = T)) #作成したデータをブラウザで表示する plot(map)
  • 16. R で オープンデータ (SPARQLもできます!) #SPARQLパッケージを導入する install.pacages(‘SPARQL’) library(SPARQL) #エンドポイントの指定 endpoint <- http://services.data.gov/sparql #クエリ query <-"PREFIX dgp1187: <http://data-gov.tw.rpi.edu/vocab/p/1187/> SELECT ?ye ?fi ?ac WHERE { ?s dgp1187:year ?ye . ?s dgp1187:fires ?fi . ?s dgp1187:acres ?ac . }" #クエリ qd <- SPARQL(endpoint,query); qd$result