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

熱線電話:13121318867

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

作者:丁點helper

來源:丁點幫你

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

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

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

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

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

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

不同類型的數(shù)據(jù)可以根據(jù)需要互相轉換,用as.目標數(shù)據(jù)類型()函數(shù):

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

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

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

之前講過,將定性變量(即分類變量)以因子的形式輸入會有助于后續(xù)的統(tǒng)計分析工作,factor()這個函數(shù)可以幫我們把數(shù)據(jù)轉變?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這樣的阿拉伯數(shù)字其實不太利于準確地表達數(shù)據(jù)內容,可以給原來的1和2加上標簽:

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

所以在輸入定性變量(分類變量)時可以采用這種簡便方法。

再看另一個例子:

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

由于status是一個有序分類變量,所以在轉變?yōu)橐蜃訒r還應體現(xiàn)其順序:

status <- factor(status, levels = c('poor', 'improved','excellent'),ordered = TRUE) 
status[1] 
poor  improved  excellent 
poor  excellentLevels:
poor < improved < excellent
這里的順序是根據(jù)levels這個命令中的內容生成的,可自行調整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這個向量為例:

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ù)計算

以age這個向量為例:

age <- c(25, 34, 59, 60, 20)
# 仍以age為例age
[1] 25 34 59 60 20
age+4       
# 給向量中每個數(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)   
# 再生成一個向量  
age+age2    
# 將兩向量中的元素相加
[1]  45  64  99 110  80

4. 生成特定形式的向量

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

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

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

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

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

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

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(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(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括: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); }