RからのSQLクエリ

Rで学ぶ中級データインポート

Filip Schouwenaars

Instructor, DataCamp

ch_2_1_slides.003.png

Rで学ぶ中級データインポート

ch_2_1_slides.004.png

Rで学ぶ中級データインポート

ch_2_1_slides.005.png

Rで学ぶ中級データインポート

ch_2_1_slides.006.png

Rで学ぶ中級データインポート

ch_2_1_slides.007.png

Rで学ぶ中級データインポート

選択的インポート

  • SQLクエリ

  • DBI -> RMySQL, RPostgreSQL, ...

  • SQLの基本のみ

Rで学ぶ中級データインポート

company

ch_2_1_slides.012.png

Rで学ぶ中級データインポート

company

ch_2_1_slides.013.png

Rで学ぶ中級データインポート

パッケージの読み込みと接続

library(DBI)con <- dbConnect(RMySQL::MySQL(),
         dbname = "company",
         host = "courses.csrrinzqubik.us-
                          east-1.rds.amazonaws.com",
         port = 3306,
         user = "student",
         password = "datacamp")
Rで学ぶ中級データインポート

例1

employees <- dbReadTable(con, "employees")
subset(employees, 
         subset = started_at > "2012-09-01", 
         select = name)
     name
3   Julie
4 Heather
5    John
dbGetQuery(con, "SELECT name FROM employees 
                      WHERE started_at > '2012-09-01'")
     name
1   Julie
2 Heather
3    John
Rで学ぶ中級データインポート

例1

ch_2_1_slides.027.png

Rで学ぶ中級データインポート

例1

ch_2_1_slides.028.png

Rで学ぶ中級データインポート

例1

ch_2_1_slides.029.png

Rで学ぶ中級データインポート

例1

ch_2_1_slides.030.png

Rで学ぶ中級データインポート

例1

ch_2_1_slides.031.png

Rで学ぶ中級データインポート

例1

ch_2_1_slides.032.png

Rで学ぶ中級データインポート

company

ch_2_1_slides.034.png

Rで学ぶ中級データインポート

company

ch_2_1_slides.035.png

Rで学ぶ中級データインポート

例2

products <- dbReadTable(con, "products")

subset(products, subset = contract == 1)
  id          name contract
2  2     Call Plus        1
4  9 Biz Unlimited        1
dbGetQuery(con, "SELECT * FROM products 
                                   WHERE contract = 1")
  id          name contract
1  2     Call Plus        1
2  9 Biz Unlimited        1
Rで学ぶ中級データインポート

練習しましょう!

Rで学ぶ中級データインポート

Preparing Video For Download...