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

熱線電話(huà):13121318867

登錄
首頁(yè)精彩閱讀R語(yǔ)言-向量構(gòu)造及函數(shù)構(gòu)造
R語(yǔ)言-向量構(gòu)造及函數(shù)構(gòu)造
2018-03-14
收藏

R語(yǔ)言-向量構(gòu)造及函數(shù)構(gòu)造

1,生成向量的方法
(1) seq()函數(shù)
[ruby] view plain copy

    > x=seq(from=1, to=5, by=0.5)  
    > x  
    # [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0  

(2)rep()函數(shù)

[ruby] view plain copy

    > x=rep(pi, times=5)  
    > x  
    # [1] 3.141593 3.141593 3.141593 3.141593 3.141593  

(3)seq 與 rep 結(jié)合使用

[ruby] view plain copy

    > x=rep(seq(from=1,to=5,by=1), times=5)  
    > x  
    # [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5  

(4)自主建立向量

[ruby] view plain copy

    > x=c(rep(seq(from=1,to=5,by=1), times=2),pi,17,24)  
    > x  
     # [1]  1.000000  2.000000  3.000000  4.000000  5.000000  1.000000  2.000000  3.000000  
     # [9]  4.000000  5.000000  3.141593 17.000000 24.000000  

2,選擇向量元素

(1)x[ i ] 形式,i表示下標(biāo)位

[ruby] view plain copy

    > x  
    # [1]  1.000000  2.000000  3.000000  4.000000  5.000000  1.000000  2.000000  3.000000  
    # [9]  4.000000  5.000000  3.141593 17.000000 24.000000  
    > x[11]  
    # [1] 3.141593  

(2)x[ m: n] 形式,選擇一段元素

[ruby] view plain copy

    > x[c(11:13)]  
    # [1]  3.141593 17.000000 24.000000  
    > x[seq(from=11,to=13,b=1)]  #用了seq函數(shù)  
    # [1]  3.141593 17.000000 24.000000  


(3)使用邏輯向量從數(shù)據(jù)向量中選擇元素

[ruby] view plain copy

    > x>3 # 邏輯判斷x的各元素  
    # [1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  
    > x[x>3] #選擇TRUE的位置的元素  
    # [1]  4.000000  5.000000  4.000000  5.000000  3.141593 17.000000 24.000000  
    > x%%2==0 #選擇奇數(shù)  
    # [1] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  
    > x[x%%2==1]   
    # [1]  1  3  5  1  3  5 17  


(4)自定義行名,取數(shù)

[ruby] view plain copy

    > year=c(1983,1982,1988,1990)  
    > names(year)=c('A','B','C','D')  
    > year  
    #  A    B    C    D   
    # 1983 1982 1988 1990   
    > year[c('A','D')]  
    # A    D   
    # 1983 1990   


3,函數(shù)編寫(xiě)

(1)if選擇函數(shù)

[ruby] view plain copy

    fun.test <- function(a, b, method = "add"){   ## function關(guān)鍵字,fun.test函數(shù)名  
        if(method == "add"){                      ## 如果if或者for/while等后面的語(yǔ)句只有一行,則無(wú)需使用花括號(hào)  
            res <- a + b                            
        }  
        if(method == "subtract"){  
            res <- a - b  
        }  
        return(res)                               ## 返回值  
    }  


[ruby] view plain copy

    > ### 檢驗(yàn)結(jié)果  
    > fun.test(a = 10, b = 8, method = "add")  
    # [1] 18  
    > ### 檢驗(yàn)結(jié)果  
    > fun.test(a = 10, b = 8, method = "subtract")  
    # [1] 2  


(2)for循環(huán)函數(shù)

[ruby] view plain copy

    ### for循環(huán)與算法  
    test.sum <- function(x)  
    {  
      res <- 0                      ## 設(shè)置初始值,在第一次循環(huán)的時(shí)候使用  
      for(i in 1:length(x)){  
        res <- res + x[i]           ## 這部分是算法的核心,總是從右面開(kāi)始計(jì)算,結(jié)果存到左邊的對(duì)象  
      }  
      return(res)  
    }  
      
      
    ### 檢驗(yàn)函數(shù)  
    a <- c(1,2,1,6,1,8,9,8)  
    test.sum(a)  
    sum(a)  

(3)return函數(shù)

[ruby] view plain copy

    ## 計(jì)算標(biāo)準(zhǔn)差  
    sd2 <- function(x)  
    {  
      # 異常處理,當(dāng)輸入的數(shù)據(jù)不是數(shù)值類(lèi)型時(shí)報(bào)錯(cuò)  
      if(!is.numeric(x)){  
        stop("the input data must be numeric!\n")  
      }  
      # 異常處理,當(dāng)僅輸入一個(gè)數(shù)據(jù)的時(shí)候,告知不能計(jì)算標(biāo)準(zhǔn)差  
      if(length(x) == 1){  
        stop("can not compute sd for one number,  
             a numeric vector required.\n")  
      }  
      ## 初始化一個(gè)臨時(shí)向量,保存循環(huán)的結(jié)果,  
      ## 求每個(gè)值與平均值的平方  
      x2 <- c()  
      ## 求該向量的平均值  
      meanx <- mean(x)  
      ## 循環(huán)  
      for(i in 1:length(x)){  
        xn <- x[i] - meanx  
        x2[i] <- xn^2  
      }  
      ## 求總平方和  
      sum2 <- sum(x2)  
      # 計(jì)算標(biāo)準(zhǔn)差  
      sd <- sqrt(sum2/(length(x)-1))  
      # 返回值  
      return(sd)  
      }  
      
    ## 程序的檢驗(yàn)  
    ## 正常的情況  
    sd2(c(2,6,4,9,12))  
    ## 一個(gè)數(shù)值的情況  
    sd2(3)  
    ## 輸入數(shù)據(jù)不為數(shù)值類(lèi)型時(shí)  
    sd2(c("1", "2"))

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

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

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

OK
客服在線
立即咨詢(xún)
客服在線
立即咨詢(xún)
') } 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, // 表示用戶(hù)后臺(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)參見(jiàn):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); }