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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀R語(yǔ)言數(shù)據(jù)格式長(zhǎng)數(shù)據(jù)和寬數(shù)據(jù)之間的轉(zhuǎn)換
R語(yǔ)言數(shù)據(jù)格式長(zhǎng)數(shù)據(jù)和寬數(shù)據(jù)之間的轉(zhuǎn)換
2018-06-20
收藏

 R語(yǔ)言數(shù)據(jù)格式長(zhǎng)數(shù)據(jù)和寬數(shù)據(jù)之間的轉(zhuǎn)換

什么是長(zhǎng)數(shù)據(jù)?

    長(zhǎng)數(shù)據(jù)一般是指數(shù)據(jù)集中的變量沒有做明確的細(xì)分,即變量中至少有一個(gè)變量中的元素存在值嚴(yán)重重復(fù)循環(huán)的情況(可以歸為幾類),表格整體的形狀為長(zhǎng)方形,即 變量少而觀察值多。例如:
    此數(shù)據(jù)集為隨機(jī)抽取6個(gè)省和其13年到15年的讀取GDP,可以看到變量year 為安裝時(shí)間順序重復(fù)的變量

    什么是寬數(shù)據(jù)?
    寬數(shù)據(jù)是指數(shù)據(jù)集對(duì)所有的變量進(jìn)行了明確的細(xì)分,各變量的值不存在重復(fù)循環(huán)的情況也無(wú)法歸類。數(shù)據(jù)總體的表現(xiàn)為 變量多而觀察值少。

    3.為什么需要轉(zhuǎn)換?
    長(zhǎng)數(shù)據(jù)與寬數(shù)據(jù)之間的轉(zhuǎn)換通常為以下兩個(gè)原因:
    時(shí)間序列數(shù)據(jù)想要觀察多個(gè)種類的變量在一段時(shí)間內(nèi)的變化,如上例,寬數(shù)據(jù)格式無(wú)法利用ggplot做出圖形。
    當(dāng)數(shù)據(jù)清洗完成后,導(dǎo)入某些軟件時(shí),例如導(dǎo)入SPSS軟件時(shí)寬數(shù)據(jù)格式會(huì)更好。
    4.轉(zhuǎn)換的方法
        使用到的包
        函數(shù)的使用方法
library(tidyr)# 使用的gather & spread
library(reshape2) # 使用的函數(shù) melt & dcast
#使用gather函數(shù)將寬數(shù)據(jù)gd1轉(zhuǎn)換為長(zhǎng)數(shù)據(jù)gd1_long
gd1_long<-gather(gd1,year,gdp,X2015年:X2006年)

#使用melt 函數(shù)將寬數(shù)據(jù)gd1轉(zhuǎn)換為長(zhǎng)數(shù)據(jù)gd1_long1
gd1_long1<-melt(gd1,
                id.vars = c('地區(qū)'),#需要保留不參與聚合的變量,
                measure.vars = c('X2015年','X2006年')#用于聚合的變量,
                variable.name='year',
                value.name='gdp')
#ps: id_vars和 measure.vars只需要制定一個(gè)即可;另外一個(gè)默認(rèn)是除指定的變量外的所有變量.

#使用spread函數(shù)將gd1_long長(zhǎng)數(shù)據(jù)轉(zhuǎn)換為寬數(shù)據(jù)gd1_wide
gd1_wide<-spread(gd1_long,year,gdp) #year為需要分解的變量,gdp為分解后的列的取值

#使用dcast函數(shù)將gd1_long長(zhǎng)數(shù)據(jù)轉(zhuǎn)換為寬數(shù)據(jù)gd1_wide1

gd1_wide1<-dcast(gd1_long1,地區(qū)~gd1_long1$year,value.var = 'gdp')

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