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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀把數(shù)據(jù)輸入R語(yǔ)言中,如何進(jìn)行簡(jiǎn)單的操作(一)
把數(shù)據(jù)輸入R語(yǔ)言中,如何進(jìn)行簡(jiǎn)單的操作(一)
2020-09-04
收藏

作者:丁點(diǎn)helper

來(lái)源:丁點(diǎn)幫你

回憶一下上一講用到的例子:

輸入數(shù)據(jù)的代碼在上一講詳細(xì)講解過(guò),這里總結(jié)如下:

age <- c(25, 34, 59, 60, 20)   
#患者年齡type <- c(1, 2, 2, 2, 1)       
#糖尿病類(lèi)型status <- c("poor", "improved", "excellent", "poor", "excellent")  
#病情comorbidity<- c(TRUE, FALSE, FALSE, TRUE, FALSE)   
#出現(xiàn)并發(fā)癥

age、type、status、comorbidity中分別僅有一種數(shù)據(jù)類(lèi)型,它們都是向量。本文介紹生成向量之后,如何對(duì)其進(jìn)行簡(jiǎn)單的操作。

1. 查看與改變向量的數(shù)據(jù)類(lèi)型
看到一個(gè)向量,首先要搞清楚其中包含的數(shù)據(jù)類(lèi)型。就本例而言,從表面看也很容易區(qū)分,但實(shí)際上一項(xiàng)統(tǒng)計(jì)分析工作用到的向量可能很多,用函數(shù)class()可以快速知曉某向量的數(shù)據(jù)類(lèi)型,例如:

class(age) [1]
"numeric"class(type) [1] 
"numeric"class(status) [1]
"character"class(comorbidity)[1]
"logical"

不同類(lèi)型的數(shù)據(jù)可以根據(jù)需要互相轉(zhuǎn)換,用as.目標(biāo)數(shù)據(jù)類(lèi)型()函數(shù):

as.numeric() 
#將括號(hào)中的內(nèi)容轉(zhuǎn)變?yōu)閿?shù)值型數(shù)據(jù)
as.character() 
#轉(zhuǎn)變?yōu)樽址蚢s.logical()  
#轉(zhuǎn)變?yōu)檫壿嬓蚢s.factor()    
#轉(zhuǎn)變?yōu)橐蜃有?/pre>

所以也可用as.character()將type中的數(shù)值型數(shù)據(jù)轉(zhuǎn)變?yōu)樽址停?

type[1] 
1 2 2 2 1class(type) [1] 
"numeric"
type <- as.character(type)   
# 注意要將新的結(jié)果賦值給typetype[1] "1" "2" "2" "2" "1"class(type)[1] "character"

之前講過(guò),將定性變量(即分類(lèi)變量)以因子的形式輸入會(huì)有助于后續(xù)的統(tǒng)計(jì)分析工作,factor()這個(gè)函數(shù)可以幫我們把數(shù)據(jù)轉(zhuǎn)變?yōu)橐蜃有停?

type <- c(1, 2, 2, 2, 1) 
type <- factor(type)
type[1] 
1 2 2 2 1
Levels: 1 2class(type)[1]
"factor"

用1和2這樣的阿拉伯?dāng)?shù)字其實(shí)不太利于準(zhǔn)確地表達(dá)數(shù)據(jù)內(nèi)容,可以給原來(lái)的1和2加上標(biāo)簽:

type <- factor(type, levels = c("1", "2"), 
               labels = c("Type 1", "Type 2")) 
type[1] 
Type 1 Type 2 Type 2 Type 2 Type 1
Levels: Type 1 Type 2

所以在輸入定性變量(分類(lèi)變量)時(shí)可以采用這種簡(jiǎn)便方法。

再看另一個(gè)例子:

status[1]
"poor"
"improved" 
"excellent"
"poor" 
"excellent"
status <- factor(status)status[1] 
poor  improved  excellent poor  excellentLevels: 
excellent improved poorclass(status)[1]
"factor"

由于status是一個(gè)有序分類(lèi)變量,所以在轉(zhuǎn)變?yōu)橐蜃訒r(shí)還應(yīng)體現(xiàn)其順序:

status <- factor(status, levels = c('poor', 'improved','excellent'),ordered = TRUE) 
status[1] 
poor  improved  excellent 
poor  excellentLevels:
poor < improved < excellent
這里的順序是根據(jù)levels這個(gè)命令中的內(nèi)容生成的,可自行調(diào)整levels命令中的順序:
status <- factor(status, levels = c('excellent','improved' ,'poor'),ordered = TRUE)
status[1]
poor improved  excellent 
poor  excellentLevels: 
excellent < improved < poor

2. 向量中的數(shù)據(jù)定位

以age這個(gè)向量為例:

age <- c(25, 34, 59, 60, 20) 
age
[1] 25 34 59 60 20

輸出向量中排在第3位的數(shù)據(jù):

age[3]
[1] 59

輸出排在1,2,5位的數(shù)據(jù):

age[c(1,2,5)]
[1] 25 34 20

輸出1至3位的數(shù)據(jù):

age[c(1:3)]  
[1] 25 34 59

3. 向量中的數(shù)據(jù)計(jì)算

以age這個(gè)向量為例:

age <- c(25, 34, 59, 60, 20)
# 仍以age為例age
[1] 25 34 59 60 20
age+4       
# 給向量中每個(gè)數(shù)都加4
[1] 29 38 63 64 24
sqrt(age)   
# 求平方根
[1] 5.000000 5.830952 7.681146 7.745967 4.472136
sort(age)   
# 給數(shù)據(jù)從低到高排序
[1] 20 25 34 59 60
sort(age, decreasing =T)    
# 給數(shù)據(jù)從高到低排序
[1] 60 59 34 25 20
age2 <- c(20,30,40,50,60)   
# 再生成一個(gè)向量  
age+age2    
# 將兩向量中的元素相加
[1]  45  64  99 110  80

4. 生成特定形式的向量

生成重復(fù)數(shù)據(jù)。用rep(x, ……),x表示要重復(fù)的內(nèi)容。

rep(1,times=5)   
#times表示重復(fù)的次數(shù)
[1] 1 1 1 1 1
rep(c(1,2),4)   
#times這個(gè)表達(dá)可以省略
[1] 1 2 1 2 1 2 1 2
rep(c(1,2),each=4)   
#each也是針對(duì)重復(fù)次數(shù)的命令
[1] 1 1 1 1 2 2 2 2

特定間隔的數(shù)據(jù)。用seq(from,to,by)這個(gè)函數(shù),from為起始值,to為終止值,by為數(shù)據(jù)之間的間隔。

seq(1,100,19)  
#from,to,by都可以省略 
[1]  1 20 39 58 77 96
seq(1,10)   
#如果不指定by的內(nèi)容,則默認(rèn)為1
[1]  1  2  3  4  5  6  7  8  9 10

下一篇介紹數(shù)據(jù)框的相關(guān)操作。

數(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, // 表示用戶后臺(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); }