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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言數(shù)據(jù)重塑
R語言數(shù)據(jù)重塑
2017-06-17
收藏

R語言數(shù)據(jù)重塑

R語言中的數(shù)據(jù)重塑是關(guān)于變化的數(shù)據(jù)分為行和列的方式。大多數(shù)R地數(shù)據(jù)處理的時候是通過將輸入的數(shù)據(jù)作為一個數(shù)據(jù)幀進行。這是很容易提取一個數(shù)據(jù)幀的行和列數(shù)據(jù),但在某些情況,當(dāng)我們需要的數(shù)據(jù)幀的格式是不同的來自收到它的格式。 R有許多函數(shù)用來分割,合并,改變行列,反之亦然在一個數(shù)據(jù)幀。

接合列和行中的數(shù)據(jù)幀

我們可以加入多個向量創(chuàng)建使用 cbind()函數(shù)返回數(shù)據(jù)幀。同時,我們也可以使用 rbind()函數(shù)合并兩個數(shù)據(jù)幀。

# Create vector objects.
city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)

# Combine above three vectors into one data frame.
addresses <- cbind(city,state,zipcode)

# Print a header.
cat("# # # # The First data frame\n")

# Print the data frame.
print(addresses)

# Create another data frame with similar columns
new.address <- data.frame(
   city = c("Lowry","Charlotte"),
   state = c("CO","FL"),
   zipcode = c("80230","33949"),
   stringsAsFactors=FALSE
)

# Print a header.
cat("# # # The Second data frame\n")

# Print the data frame.
print(new.address)

# Combine rows form both the data frames.
all.addresses <- rbind(addresses,new.address)

# Print a header.
cat("# # # The combined data frame\n")

# Print the result.
print(all.addresses)

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

# # # # The First data frame
     city       state zipcode
[1,] "Tampa"    "FL"  "33602"
[2,] "Seattle"  "WA"  "98104"
[3,] "Hartford" "CT"  "6161"
[4,] "Denver"   "CO"  "80294"
# # # The Second data frame
       city state zipcode
1     Lowry    CO   80230
2 Charlotte    FL   33949
# # # The combined data frame
       city state zipcode
1     Tampa    FL   33602
2   Seattle    WA   98104
3  Hartford    CT    6161
4    Denver    CO   80294
5     Lowry    CO   80230
6 Charlotte    FL   33949

合并數(shù)據(jù)幀

我們可以通過使用 merge()函數(shù)合并兩個數(shù)據(jù)幀。該數(shù)據(jù)幀必須在其上合并發(fā)生相同的列名。

在下面的例子中,我們考慮對皮馬印第安人婦女的糖尿病在可用的數(shù)據(jù)集庫名稱 "MASS". 我們合并基礎(chǔ)血壓(“BP”)和身體質(zhì)量指數(shù)(“BMI”)的值,兩個數(shù)據(jù)集。上用于合并選擇這兩列,其中,這兩個變量的值匹配在兩個數(shù)據(jù)集組合在一起的記錄,以形成一個單一的數(shù)據(jù)幀。

library(MASS)
merged.Pima <- merge(x=Pima.te, y=Pima.tr,
                    by.x=c("bp", "bmi"),
                    by.y=c("bp", "bmi")
)
print(merged.Pima)
nrow(merged.Pima)

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

   bp  bmi npreg.x glu.x skin.x ped.x age.x type.x npreg.y glu.y skin.y ped.y
1  60 33.8       1   117     23 0.466    27     No       2   125     20 0.088
2  64 29.7       2    75     24 0.370    33     No       2   100     23 0.368
3  64 31.2       5   189     33 0.583    29    Yes       3   158     13 0.295
4  64 33.2       4   117     27 0.230    24     No       1    96     27 0.289
5  66 38.1       3   115     39 0.150    28     No       1   114     36 0.289
6  68 38.5       2   100     25 0.324    26     No       7   129     49 0.439
7  70 27.4       1   116     28 0.204    21     No       0   124     20 0.254
8  70 33.1       4    91     32 0.446    22     No       9   123     44 0.374
9  70 35.4       9   124     33 0.282    34     No       6   134     23 0.542
10 72 25.6       1   157     21 0.123    24     No       4    99     17 0.294
11 72 37.7       5    95     33 0.370    27     No       6   103     32 0.324
12 74 25.9       9   134     33 0.460    81     No       8   126     38 0.162
13 74 25.9       1    95     21 0.673    36     No       8   126     38 0.162
14 78 27.6       5    88     30 0.258    37     No       6   125     31 0.565
15 78 27.6      10   122     31 0.512    45     No       6   125     31 0.565
16 78 39.4       2   112     50 0.175    24     No       4   112     40 0.236
17 88 34.5       1   117     24 0.403    40    Yes       4   127     11 0.598
   age.y type.y
1     31     No
2     21     No
3     24     No
4     21     No
5     21     No
6     43    Yes
7     36    Yes
8     40     No
9     29    Yes
10    28     No
11    55     No
12    39     No
13    39     No
14    49    Yes
15    49    Yes
16    38     No
17    28     No
[1] 17

熔化和轉(zhuǎn)換

R語言編程的最有趣的地方是關(guān)于改變多個步驟中的數(shù)據(jù)的形狀來獲得所希望的形狀。用來做這種函數(shù)被稱為 melt() 和 cast()。

我們認為數(shù)據(jù)集被稱為 ships 出現(xiàn)在庫被稱為 "MASS".

library(MASS)
print(ships)

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

   type year period service incidents
1     A   60     60     127         0
2     A   60     75      63         0
3     A   65     60    1095         3
4     A   65     75    1095         4
5     A   70     60    1512         6
.............
.............
8     A   75     75    2244        11
9     B   60     60   44882        39
10    B   60     75   17176        29
11    B   65     60   28609        58
............
............
17    C   60     60    1179         1
18    C   60     75     552         1
19    C   65     60     781         0
............
............

融化數(shù)據(jù)

現(xiàn)在,我們?nèi)诨瘮?shù)據(jù)需要組織其轉(zhuǎn)換類型(type), 并且 year 到多行以外的所有列。

molten.ships <- melt(ships, id = c("type","year"))
print(molten.ships)

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

    type year  variable value
1      A   60    period    60
2      A   60    period    75
3      A   65    period    60
4      A   65    period    75
............
............
9      B   60    period    60
10     B   60    period    75
11     B   65    period    60
12     B   65    period    75
13     B   70    period    60
...........
...........
41     A   60   service   127
42     A   60   service    63
43     A   65   service  1095
...........
...........
70     D   70   service  1208
71     D   75   service     0
72     D   75   service  2051
73     E   60   service    45
74     E   60   service     0
75     E   65   service   789
...........
...........
101    C   70 incidents     6
102    C   70 incidents     2
103    C   75 incidents     0
104    C   75 incidents     1
105    D   60 incidents     0
106    D   60 incidents     0
...........
...........

轉(zhuǎn)換數(shù)據(jù)

我們可以轉(zhuǎn)化數(shù)據(jù)轉(zhuǎn)換成在創(chuàng)建每種類型的 ships 每年的匯總的新形式。它是通過使用 case()函數(shù)。

recasted.ship <- cast(molten.ships, type+year~variable,sum)
print(recasted.ship)

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

   type year period service incidents
1     A   60    135     190         0
2     A   65    135    2190         7
3     A   70    135    4865        24
4     A   75    135    2244        11
5     B   60    135   62058        68
6     B   65    135   48979       111
7     B   70    135   20163        56
8     B   75    135    7117        18
9     C   60    135    1731         2
10    C   65    135    1457         1
11    C   70    135    2731         8
12    C   75    135     274         1
13    D   60    135     356         0
14    D   65    135     480         0
15    D   70    135    1557        13
16    D   75    135    2051         4
17    E   60    135      45         0
18    E   65    135    1226        14
19    E   70    135    3318        17
20    E   75    135     542         1

數(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); }