R 数据库
数据是关系数据库系统以规范化格式存储。因此,要进行统计计算,我们将需要非常高级和复杂的 Sql 查询。但是 R 可以轻松连接到许多关系数据库,如 MySql、Oracle、Sql server 等,并从它们中获取记录作为数据框。一旦数据在 R 环境中可用,它就成为一个普通的 R 数据集,可以使用所有强大的包和函数进行操作或分析。
在本教程中,我们将使用 MySql 作为连接 R 的参考数据库。
RMySQL 包
R 有一个名为"RMySQL"的内置包,它提供与 MySql 数据库之间的本地连接。您可以使用以下命令在 R 环境中安装此包。
install.packages("RMySQL")
将 R 连接到 MySql
安装包后,我们在 R 中创建一个连接对象以连接到数据库。它以用户名、密码、数据库名称和主机名作为输入。
# Create a connection Object to MySQL database.
# We will connect to the sampel database named "sakila" that comes with MySql installation.
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '', dbname = 'sakila',
host = 'localhost')
# List the tables available in this database.
dbListTables(mysqlconnection)
当我们执行上面的代码时,它会产生以下结果-
[1] "actor" "actor_info"
[3] "address" "category"
[5] "city" "country"
[7] "customer" "customer_list"
[9] "film" "film_actor"
[11] "film_category" "film_list"
[13] "film_text" "inventory"
[15] "language" "nicer_but_slower_film_list"
[17] "payment" "rental"
[19] "sales_by_film_category" "sales_by_store"
[21] "staff" "staff_list"
[23] "store"
查询表
我们可以使用函数
dbSendQuery()查询MySql中的数据库表。查询在 MySql 中执行,结果集使用 R
fetch() 函数返回。最后将其作为数据框存储在 R 中。
# Query the "actor" tables to get all the rows.
result = dbSendQuery(mysqlconnection, "select * from actor")
# Store the result in a R data frame object. n = 5 is used to fetch first 5 rows.
data.frame = fetch(result, n = 5)
print(data.fame)
当我们执行上面的代码时,它会产生以下结果-
actor_id first_name last_name last_update
1 1 PENELOPE GUINESS 2006-02-15 04:34:33
2 2 NICK WAHLBERG 2006-02-15 04:34:33
3 3 ED CHASE 2006-02-15 04:34:33
4 4 JENNIFER DAVIS 2006-02-15 04:34:33
5 5 JOHNNY LOLLOBRIGIDA 2006-02-15 04:34:33
使用过滤器子句查询
我们可以通过任何有效的选择查询来获取结果。
result = dbSendQuery(mysqlconnection, "select * from actor where last_name = 'TORN'")
# Fetch all the records(with n =-1) and store it as a data frame.
data.frame = fetch(result, n =-1)
print(data)
当我们执行上面的代码时,它会产生以下结果-
actor_id first_name last_name last_update
1 18 DAN TORN 2006-02-15 04:34:33
2 94 KENNETH TORN 2006-02-15 04:34:33
3 102 WALTER TORN 2006-02-15 04:34:33
更新表中的行
我们可以通过将更新查询传递给 dbSendQuery() 函数来更新 Mysql 表中的行。
dbSendQuery(mysqlconnection, "update mtcars set disp = 168.5 where hp = 110")
执行上述代码后,我们可以在MySql环境中看到更新的表。
在表格中插入数据
dbSendQuery(mysqlconnection,
"insert into mtcars(row_names, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)
values('New Mazda RX4 Wag', 21, 6, 168.5, 110, 3.9, 2.875, 17.02, 0, 1, 4, 4)"
)
执行上述代码后,我们可以在MySql环境中看到插入到表中的行。
在 MySql 中创建表
我们可以使用函数
dbWriteTable() 在 MySql 中创建表。如果表已经存在,它会覆盖该表并将数据框作为输入。
# Create the connection object to the database where we want to create the table.
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '', dbname = 'sakila',
host = 'localhost')
# Use the R data frame "mtcars" to create the table in MySql.
# All the rows of mtcars are taken inot MySql.
dbWriteTable(mysqlconnection, "mtcars", mtcars[, ], overwrite = true)
执行上述代码后,我们可以看到在MySql环境中创建的表。
在 MySql 中删除表
我们可以删除 MySql 数据库中的表,将 drop table 语句传递到 dbSendQuery() 中,就像我们使用它从表中查询数据一样。
dbSendQuery(mysqlconnection, 'drop table if exists mtcars')
执行上述代码后,我们可以看到该表在MySql环境中被删除了。