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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀R語(yǔ)言:排序問題
R語(yǔ)言:排序問題
2017-04-25
收藏

R語(yǔ)言:排序問題

數(shù)據(jù)排序
1、sort(),rank(),order()函數(shù)
Sort
排序(默認(rèn)升序,decreasing=T時(shí)為降序)
Order
排序(默認(rèn)升序,decreasing=T時(shí)為降序)
在R中,和排序相關(guān)的函數(shù)主要有三個(gè):sort(),rank(),order()。
    sort(x)是對(duì)向量x進(jìn)行排序,返回值排序后的數(shù)值向量。rank()是求秩的函數(shù),它的返回值是這個(gè)向量中對(duì)應(yīng)元素的“排名”。而order()的返回值是對(duì)應(yīng)“排名”的元素所在向量中的位置。

    下面以一小段R代碼來舉例說明:
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
    x<-c(97,93,85,74,32,100,99,67)  
    sort(x)  
    [1]  32  67  74  85  93  97  99 100  
    order(x)  #order()的返回值是各個(gè)排名的學(xué)生成績(jī)所在向量中的位置  
    [1] 5 8 4 3 2 1 7 6  
    rank(x)  #rank()的返回值是這組學(xué)生所對(duì)應(yīng)的排名  
    [1] 6 5 4 3 1 8 7 2 
深入理解一下:
sort()在單變量排序中,效果較好;
order()≈原序號(hào)(sort())  因?yàn)榭梢詷?biāo)記排序好之后的下標(biāo),在數(shù)據(jù)框中的排序操作,實(shí)用性超強(qiáng),可以實(shí)現(xiàn):
1、整個(gè)數(shù)據(jù)集按照某個(gè)變量(比如:按月份大?。┡判颍?br /> 2、整個(gè)數(shù)據(jù)集其中某個(gè)變量依據(jù)第二個(gè)變量(比如:月份)排序。
[plain] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    iris;iris[1:10,]  
    names(iris) 
    #單數(shù)據(jù)列,兩者相同  
    sort(iris$Sepal.Length)  
    iris$Sepal.Length[order(iris$Sepal.Length)]  
    #多數(shù)據(jù)列,order有奇效  
    iris[order(iris$setosa),]                 #按照setosa的大小,重排整個(gè)數(shù)據(jù)集  
    iris$Sepal.Length[order(iris$setosa)]     #按照照setosa的大小,重排Sepal.Length數(shù)據(jù)列  
    iris[order(iris$setosa),]$Sepal.Length    #與上句異曲同工 
與which有一些地方的相似,which可以實(shí)現(xiàn)返回服從條件觀測(cè)的行數(shù)。which又與subset子集篩選有關(guān)。(詳見which、subset子集篩選用法)
[plain] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    data$V1[which(data$V2<0)]                 #篩選出V1中,V2小于0的數(shù)字,跟order的作用些許相似  
    #order用法  
    iris$Sepal.Length[order(iris$setosa)]     #按照照setosa的大小,重排Sepal.Length數(shù)據(jù)列    
2、dplyr包的一些應(yīng)用
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
    #dplyr中基本函數(shù) arrange——數(shù)據(jù)排序  
    Hdma_dat[order(Hdma_dat$survived),] #傳統(tǒng)方法用order排序  
    arrange(Hdma_dat,survived) #將survived從小到大排序  
    arrange(Hdma_dat,desc(survived) #將survived從大到小排序  
    arrange(Hdma_dat,pclass,desc(survived) #先將pclass從小到大排序,再在那個(gè)數(shù)據(jù)基礎(chǔ)上讓survived從大到小排序 
使用場(chǎng)景(我經(jīng)常搞錯(cuò)...):
    譬如我想知道一組數(shù):b = c(0.9984616870 ,1.0177739597 ,0.0004250664 ,0.0015771710, 1.0177739597)
場(chǎng)景一:最大值的位置信息,我想拿到的數(shù)據(jù)是每個(gè)數(shù)值對(duì)應(yīng)的大小次序,升序,理應(yīng)(3 4.5 1 2 4.5)
那么:
[html] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    rank(b)  
    order(b) 
如果降序,就不一樣了:
[html] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    > order(c(0.9984616870 ,1.0177739597 ,0.0004250664 ,0.0015771710, 1.0177739597) ,decreasing = T)  
    [1] 2 5 1 4 3 
order=rank+sort功能=次序信息(rank)+次序排序(sort)
接下來的兩個(gè)用法是我很容易搞錯(cuò)的:
我想拿到 降序 + 不排序的次序信息,rank不適合;
               降序 + 排序的次序信息,order適合
若:
(1)按照某行順序從大到小重排另一行:
[html] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    data$x1[order(data$x2)] 
(2)按照某行最大值對(duì)位的另一行:
[html] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    data$x1[order(data$x2)[1] ] 
第二種辦法:
[html] view plain copy
print?在CODE上查看代碼片派生到我的代碼片
    data$x1[rank(data$x2) == max值] 
注意區(qū)別。

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