日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

r语言教程w3c,R语言 数据库

發(fā)布時間:2023/12/10 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 r语言教程w3c,R语言 数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)是關(guān)系數(shù)據(jù)庫系統(tǒng)以規(guī)范化格式存儲。 因此,要進行統(tǒng)計計算,我們將需要非常先進和復(fù)雜的Sql查詢。 但R語言可以輕松地連接到許多關(guān)系數(shù)據(jù)庫,如MySql,Oracle,Sql服務(wù)器等,并從它們獲取記錄作為數(shù)據(jù)框。 一旦數(shù)據(jù)在R語言環(huán)境中可用,它就變成正常的R語言數(shù)據(jù)集,并且可以使用所有強大的包和函數(shù)來操作或分析。

在本教程中,我們將使用MySql作為連接到R語言的參考數(shù)據(jù)庫。

RMySQL包

R語言有一個名為“RMySQL”的內(nèi)置包,它提供與MySql數(shù)據(jù)庫之間的本地連接。 您可以使用以下命令在R語言環(huán)境中安裝此軟件包。install.packages("RMySQL")

將R連接到MySql

一旦安裝了包,我們在R中創(chuàng)建一個連接對象以連接到數(shù)據(jù)庫。 它使用用戶名,密碼,數(shù)據(jù)庫名稱和主機名作為輸入。# 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)

當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -[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"

查詢表

我們可以使用函數(shù)dbSendQuery()查詢MySql中的數(shù)據(jù)庫表。 查詢在MySql中執(zhí)行,并使用R語言fetch()函數(shù)返回結(jié)果集。 最后,它被存儲為R語言中的數(shù)據(jù)幀。# 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)

當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -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查詢來獲取結(jié)果。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)

當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -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()函數(shù)來更新Mysql表中的行。dbSendQuery(mysqlconnection, "update mtcars set disp = 168.5 where hp = 110")

在執(zhí)行上面的代碼后,我們可以看到在MySql環(huán)境中更新的表。

將數(shù)據(jù)插入表中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)"

)

在執(zhí)行上面的代碼后,我們可以看到插入到MySql環(huán)境中的表中的行。

在MySql中創(chuàng)建表

我們可以在MySql中使用函數(shù)dbWriteTable()創(chuàng)建表。 如果表已經(jīng)存在,它將覆蓋該表,并將數(shù)據(jù)幀用作輸入。# 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)

執(zhí)行上面的代碼后,我們可以看到在MySql環(huán)境中創(chuàng)建的表。

刪除MySql中的表

我們可以刪除MySql數(shù)據(jù)庫中的表,將drop table語句傳遞到dbSendQuery()中,就像我們使用它查詢表中的數(shù)據(jù)一樣。dbSendQuery(mysqlconnection, 'drop table if exists mtcars')

執(zhí)行上面的代碼后,我們可以看到表在MySql環(huán)境中被刪除。

總結(jié)

以上是生活随笔為你收集整理的r语言教程w3c,R语言 数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。