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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言函數(shù)
R語言函數(shù)
2017-06-16
收藏

R語言函數(shù)

函數(shù)是一個(gè)組織在一起的一組以執(zhí)行特定任務(wù)的語句。R語言有大量的內(nèi)置函數(shù),用戶也可以創(chuàng)建自己的函數(shù)。

R語言中的函數(shù)是一個(gè)對(duì)象,所以R語言解釋器為能夠通過控制到該函數(shù),帶有參數(shù)可能是函數(shù)必要完成的操作。

反過來函數(shù)執(zhí)行其任務(wù),并將控制返回到其可以被存儲(chǔ)在其它的目的解釋器以及任何結(jié)果。

函數(shù)定義

R函數(shù)是通過使用關(guān)鍵字 function 來創(chuàng)建。R函數(shù)的定義基本語法如下:function_name <- function(arg_1, arg_2, ...) { Function body }

函數(shù)組件

函數(shù)的不同部分是:

函數(shù)名稱: 這是函數(shù)的實(shí)際名稱。它被存入R環(huán)境作為一個(gè)對(duì)象使用此名稱。

參數(shù):  參數(shù)是一個(gè)占位符。當(dāng)調(diào)用一個(gè)函數(shù),傳遞一個(gè)值到參數(shù)。參數(shù)是可選的; 也就是說,一個(gè)函數(shù)可以含有任何參數(shù)。此外參數(shù)可以有默認(rèn)值。

函數(shù)體: 函數(shù)體包含定義函數(shù)是使用來做什么的語句集合。

返回值: 一個(gè)函數(shù)的返回值是在函數(shù)體中評(píng)估計(jì)算最后一個(gè)表達(dá)式的值。

示例

R具有許多內(nèi)置函數(shù)可直接在程序中調(diào)用而不先定義它們。我們也可以創(chuàng)建和使用稱為用戶自定義函數(shù),如那些我們自己定義的函數(shù)。

內(nèi)置函數(shù)

內(nèi)建函數(shù)的簡單例子如:seq(), mean(), max(), sum(x) 和 paste(...) 等等. 它們被直接由用戶編寫的程序調(diào)用??梢詤⒖甲顝V泛用 在R編程里面的函數(shù)。

# Create a sequence of numbers from 32 to 44.
print(seq(32,44))

# Find mean of numbers from 25 to 82.
print(mean(25:82))

# Find sum of numbers frm 41 to 68.
print(sum(41:68))

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526

用戶定義函數(shù)

我們可以在R語言中創(chuàng)建用戶定義的函數(shù),它們是特定于用戶想要實(shí)現(xiàn)什么功能,一旦創(chuàng)建了它們可以像內(nèi)置函數(shù)一樣使用。下面是函數(shù)如何創(chuàng)建和使用的一個(gè)例子。

# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
  for(i in 1:a) {
        b <- i^2
        print(b)
        }
              }   

調(diào)用函數(shù)

# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
  for(i in 1:a) {
        b <- i^2
        print(b)
        }
              }

# Call the function new.function supplying 6 as an argument.
new.function(6)

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

調(diào)用函數(shù)不帶參數(shù)

# Create a function without an argument.
new.function <- function() {
        for(i in 1:5) {
        print(i^2)
        }
              }    

# Call the function without supplying an argument.
new.function()

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

調(diào)用函數(shù)帶有參數(shù)值(按位置和名稱)

參數(shù)在傳到函數(shù)調(diào)用可以以相同的順序如提供在函數(shù)定義的順序一樣,或者它們可以以不同的順序提供(按參數(shù)名稱)。

# Create a function with arguments.
new.function <- function(a,b,c) {
        result <- a*b+c
        print(result)
                 }

# Call the function by position of arguments.
new.function(5,3,11)

# Call the function by names of the arguments.
new.function(a=11,b=5,c=3)

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 26
[1] 58

帶有調(diào)用默認(rèn)參數(shù)的函數(shù)

我們可以在函數(shù)定義中定義的參數(shù)的值并調(diào)用該函數(shù),而不提供任何參數(shù)來獲取默認(rèn)參數(shù)的結(jié)果。但是,我們也可以通過提供參數(shù)的新值調(diào)用來這些函數(shù),并得到非默認(rèn)的結(jié)果。

# Create a function with arguments.
new.function <- function(a = 3,b =6) {
        result <- a*b
        print(result)
                     }

# Call the function without giving any argument.
new.function()

# Call the function with giving new values of the argument.
new.function(9,5)

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 18
[1] 45

函數(shù)延遲計(jì)算

函數(shù)的參數(shù)在延遲方式計(jì)算,這意味著只有在需要函數(shù)體時(shí),它們才會(huì)進(jìn)行評(píng)估計(jì)算。

# Create a function with arguments.
new.function <- function(a, b) {
       print(a^2)
       print(a)
       print(b)
                }

# Evaluate the function without supplying one of the arguments.
new.function(6)

當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:

[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default

數(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)檢測極驗(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ù)說明請(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); }