R 数据导入进阶
Filip Schouwenaars
Instructor, DataCamp





SQL 查询
DBI -> RMySQL、RPostgreSQL 等
只用 SQL 基础


library(DBI)con <- dbConnect(RMySQL::MySQL(),
dbname = "company",
host = "courses.csrrinzqubik.us-
east-1.rds.amazonaws.com",
port = 3306,
user = "student",
password = "datacamp")
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








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 数据导入进阶