R語言對數(shù)據(jù)庫進(jìn)行操作的實例詳解
數(shù)據(jù)是關(guān)系數(shù)據(jù)庫系統(tǒng)以規(guī)范化格式存儲。 因此,要進(jìn)行統(tǒng)計計算,我們將需要非常先進(jìn)和復(fù)雜的Sql查詢。 但R語言可以輕松地連接到許多關(guān)系數(shù)據(jù)庫,如MySql,Oracle,Sql服務(wù)器等,并從它們獲取記錄作為數(shù)據(jù)框。 一旦數(shù)據(jù)在R語言環(huán)境中可用,它就變成正常的R語言數(shù)據(jù)集,并且可以使用所有強(qiáng)大的包和函數(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ù)庫名稱和主機(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)
當(dāng)我們執(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)
當(dāng)我們執(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)
當(dāng)我們執(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)境中被刪除。
到此這篇關(guān)于R語言對數(shù)據(jù)庫進(jìn)行操作的實例詳解的文章就介紹到這了,更多相關(guān)R語言數(shù)據(jù)庫操作方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式
這篇文章主要介紹了R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04聊聊R語言中Legend 函數(shù)的參數(shù)用法
這篇文章主要介紹了聊聊R語言中Legend 函數(shù)的參數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03R語言可視化開發(fā)forestplot根據(jù)分組設(shè)置不同顏色
這篇文章主要為大家介紹了R語言可視化開發(fā)使用forestplot根據(jù)分組設(shè)置不同顏色的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05R語言編程數(shù)學(xué)分析重讀微積分微分學(xué)原理運(yùn)用
這篇文章主要介紹了R語言編程數(shù)學(xué)分析重讀微積分微分學(xué)的原理運(yùn)用,有需要的朋友可以借鑒參考下,希望能夠有=有所幫助,祝大家多多進(jìn)步2021-10-10