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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言-數(shù)組到矩陣的轉(zhuǎn)換
R語言-數(shù)組到矩陣的轉(zhuǎn)換
2018-03-14
收藏

R語言-數(shù)組到矩陣的轉(zhuǎn)換

1、問題:

有一個很大的三維數(shù)組,需要轉(zhuǎn)換為一個矩陣,是否能在R中用循環(huán)語句或者其他方式實現(xiàn)?

三維數(shù)組(3, 2, 3)類似下面形式:
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

, , 3

     [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18


希望轉(zhuǎn)換后的矩陣(6, 3)如下:
1        7        13
4        10        16
2        8        14
5        11        17
3        9        15
6        12        18

2、解答:

基于問題數(shù)據(jù)的特點,可直接用行組合就可以,避免使用循環(huán)計算,在進行大數(shù)據(jù)處理時可顯著提高處理效率。

可以看到最終數(shù)據(jù)呈橫向擴展,而與第3維數(shù)據(jù)的個數(shù)無關(guān)。

1、假定有數(shù)據(jù):

> a <- array(1:18, dim=c(3,2,3))
> a
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

, , 3

     [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18

2、合成后的矩陣為:

>b<- rbind(a[1,,],a[2,,],a[3,,])
一句話搞定。

3、查看結(jié)果

> b
     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    4   10   16
[3,]    2    8   14
[4,]    5   11   17
[5,]    3    9   15
[6,]    6   12   18

4、使用更多數(shù)據(jù)測試:

> a <- array(1:24, dim=c(3,2,4))
> a
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

, , 3

     [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18

, , 4

     [,1] [,2]
[1,]   19   22
[2,]   20   23
[3,]   21   24

> b<-rbind(a[1,,],a[2,,],a[3,,])
> b
     [,1] [,2] [,3] [,4]
[1,]    1    7   13   19
[2,]    4   10   16   22
[3,]    2    8   14   20
[4,]    5   11   17   23
[5,]    3    9   15   21
[6,]    6   12   18   24

3、另外的方法

1、apply()

apply(x, 3, t)

apply()函數(shù),可將一個任意函數(shù)“應(yīng)用”到矩陣、數(shù)組、數(shù)據(jù)框的任何維度上。apply函數(shù)的使用格式為:
 apply(x, MARGIN, FUN, ...)
其中,x為數(shù)據(jù)對象,MARGIN是維度的下標(biāo),F(xiàn)UN是由你指定的函數(shù),而...則包括了任何想傳遞給FUN的參數(shù)。在矩陣或數(shù)據(jù)框中,MARGIN=1表示行,MARGIN=2表示列。

2、aperm()

(1)aperm() 函數(shù),Transpose an array by permuting its dimensions and optionally resizingit.

Transpose變換順序

permute 序列改變,重新排列一個數(shù)組

該函數(shù)意即改變數(shù)組的維度順序,維度1,2,3按不同順序進行變換。

(2)array()函數(shù),用法array(data = NA, dim = length(data), dimnames = NULL)

array(aperm(x, c(2,1,3)), c(6,3))

將數(shù)組x維度改變(1->2,2->1,3->3)后:

aperm(x, c(2,1,3))

再變換成新的數(shù)組:

array(aperm(x, c(2,1,3)), c(6,3))

注意:

其實這樣做有點多余,可直接應(yīng)用數(shù)組變換:

array(x, c(6,3))

結(jié)果與上述方法結(jié)果一樣。

如果是三維數(shù)量是4,則公式為:

array(x,c(6,4))

依此類推。

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

若不方便掃碼,搜微信號: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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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 = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+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); }