R在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
尝试说明图片 发布于:2024-04-10 13:37 --- 比较公式和循环计算平均值的流逝时间 发布于:2024-03-10 19:24 农业R语言统计 发布于:2023-12-14 14:02 朱兴垚-202105002605 发布于:2023-12-03 23:45 R demo 发布于:2023-10-06 16:14 小姑父的箱线图 发布于:2023-07-28 17:33 第一章第二章 发布于:2023-06-12 11:01 R入门预备知识 发布于:2023-06-09 15:04 创建一个数轴 发布于:2023-06-07 15:48 生成随机数 发布于:2023-05-10 17:10 读取gct文件 发布于:2023-05-06 11:08 excel画图 发布于:2023-02-09 18:17 储存经纬度的数组 发布于:2023-01-28 16:20 计算,logp函数,修改 发布于:2022-11-15 16:41 用 Monte Carlo 方法进行 概率和分位计算 发布于:2022-11-01 09:49 haoyong hho 发布于:2022-10-25 20:36 数理统计大作业代码 发布于:2022-10-24 16:44 画图——df=12的t分布 发布于:2022-10-23 19:18 统计作图题 发布于:2022-10-24 18:39 统计分析题 发布于:2022-10-23 16:39 ISYE 6501 HW8 (11.1) 发布于:2022-10-17 10:48 城市广告市场案例 发布于:2022-10-07 15:32 画sin()函数图像 发布于:2022-08-24 17:10 我的测试代码 发布于:2022-08-10 10:23 离散卷积逆 发布于:2022-06-20 17:03 定积分直接求和 发布于:2022-06-20 03:29 Buffon's needle problem 发布于:2022-06-03 12:48 试运行输出 发布于:2022-05-06 08:54 计算向量中的两两差值 发布于:2022-05-02 16:52 30日晚上10:00 发布于:2022-05-01 10:38 毕业论文代码 发布于:2022-03-06 23:18 中级计量模拟实验1 发布于:2021-11-18 14:15 第一个demo,不知道啥环境。 发布于:2021-11-09 09:18 R语言项目实验 发布于:2021-09-27 17:02 矩阵中的公式 发布于:2021-04-22 10:55 方块地图骰子判定 发布于:2021-01-10 17:24 R语言Hello World 发布于:2020-08-23 14:07 R语言Hello World 发布于:2020-08-23 14:07 R语言Hello World 发布于:2020-08-04 10:53 [更多]
显示目录

数据库



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

数据库

数据是关系系统以规范化格式存储。 因此,要进行统计计算,我们将需要非常先进和复杂的Sql查询。 但R语言可以轻松地连接到许多关系数据库,如MySql,Oracle,Sql服务器等,并从它们获取记录作为数据框。 一旦数据在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.frame)

当我们执行上面的代码,它产生以下结果 -

 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

带过滤条件的查询

我们可以传递任何有效的select查询来获取结果。

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中创建表

我们可以在MySql中使用函数dbWriteTable()创建表。 如果表已经存在,它将覆盖该表,并将数据帧用作输入。

# 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环境中被删除。


由JSRUN为你提供的R在线运行、在线编译工具
        JSRUN提供的R 在线运行,R 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout