99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀R語(yǔ)言數(shù)據(jù)庫(kù)(MySQL)
R語(yǔ)言數(shù)據(jù)庫(kù)(MySQL)
2017-06-19
收藏

R語(yǔ)言數(shù)據(jù)庫(kù)(MySQL)

數(shù)據(jù)是關(guān)系數(shù)據(jù)庫(kù)系統(tǒng)中存儲(chǔ)的統(tǒng)一化格式。 因此,實(shí)施我們需要非常先進(jìn)和復(fù)雜的SQL查詢統(tǒng)計(jì)計(jì)算。但是R能夠輕松地連接到諸如MySql, Oracle, Sql server等多種關(guān)系數(shù)據(jù)庫(kù)并且可以從它們的記錄轉(zhuǎn)為R中的數(shù)據(jù)幀。一旦數(shù)據(jù)是在R環(huán)境中可用,就變成了正常R數(shù)據(jù)集,并可以被操縱或使用所有強(qiáng)大包和函數(shù)來(lái)進(jìn)行分析。

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

RMySQL 軟件包

R有一個(gè)名為“RMySQL”它提供了與 MySQL 數(shù)據(jù)庫(kù)之間的本地連接的內(nèi)置軟件包??梢允褂孟旅娴拿顏?lái)安裝這個(gè)包到 R 的環(huán)境。

install.packages("RMySQL")

連接R到MySql

一旦軟件包安裝,我們創(chuàng)建 R 的連接對(duì)象連接到數(shù)據(jù)庫(kù)。這需要用戶名,密碼,數(shù)據(jù)庫(kù)名和主機(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í)行時(shí),它產(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()查詢?cè)贛ySQL數(shù)據(jù)庫(kù)表。查詢獲取執(zhí)行在MySQL中并使用fetch()函數(shù)返回結(jié)果集。最后,它被存儲(chǔ)為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.fame)

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(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

查詢與篩選子句

我們可以通過(guò)任何有效的 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í)行時(shí),它產(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

更新表的行

我們可以通過(guò)傳遞更新查詢到dbSendQuery()函數(shù)更新一個(gè)MySQL表中的行。

dbSendQuery(mysqlconnection, "update mtcars set disp = 168.5 where hp = 110")

在執(zhí)行上面的代碼后,我們可以看到該表在MySQL環(huán)境中已經(jīng)更新。

數(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)建表

我們可以使用函數(shù)dbWriteTable()創(chuàng)建一個(gè)表在MySQL中。它覆蓋表,如果它已經(jīng)存在,并且需要一個(gè)數(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ù)庫(kù)這個(gè)表刪除,通過(guò) DROP TABLE 語(yǔ)句發(fā)送到 dbSendQuery(),與之前從表查詢數(shù)據(jù)的方式相同。

dbSendQuery(mysqlconnection, 'drop table if exists mtcars')

在執(zhí)行上面的代碼后,我們可以看到該表在MySQL環(huán)境被丟棄。


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }