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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言數(shù)據(jù)類型
R語言數(shù)據(jù)類型
2017-06-14
收藏


R語言數(shù)據(jù)類型

當(dāng)編寫任何編程語言程序,需要使用不同的變量來存儲各種信息。變量不過是用于保留存儲器位置的存儲值。這意味著,當(dāng)創(chuàng)建一個變量,它會保留在內(nèi)存中的一些空間。

你可能喜歡存儲諸如字符以外的數(shù)據(jù)類型,如:寬字符,整型,浮點(diǎn)型,雙浮點(diǎn)型,布爾等信息?;谧兞康?a href='/map/shujuleixing/' style='color:#000;font-size:inherit;'>數(shù)據(jù)類型,操作系統(tǒng)分配內(nèi)存,并決定什么可以存儲在存儲器。

在其他編程語言中,如C和JavaR中的變量沒有聲明為某些數(shù)據(jù)類型。變量分配R-對象和R對象的數(shù)據(jù)類型變?yōu)樽兞康?a href='/map/shujuleixing/' style='color:#000;font-size:inherit;'>數(shù)據(jù)類型。有許多類型的R-對象。常用的有:

矢量

列表

矩陣

數(shù)組

因子

數(shù)據(jù)幀

這些對象的是最簡單的矢量對象并且這些原子矢量有六種數(shù)據(jù)類型,也被稱為六類向量。另外R-對象是建立在原子向量。


因此,在R語言中的非?;镜?a href='/map/shujuleixing/' style='color:#000;font-size:inherit;'>數(shù)據(jù)類型是R-對象,如上圖所示占據(jù)著不同類別的元素向量。請注意R語言中類的數(shù)量并不只限于上述的六種類型。 例如,我們可以使用許多原子向量以及創(chuàng)建一個數(shù)組,它的類將成為數(shù)組。

向量

當(dāng)您希望使用多個元素創(chuàng)建向量,應(yīng)該使用c()函數(shù),這意味著元素結(jié)合成一個向量。

# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.
print(class(apple))

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

[1] "red"    "green"  "yellow"
[1] "character"

列表

列表是R-對象,它里面可以包含多個不同類型的元素,如向量,函數(shù),甚至是另一個列表。

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.
print(list1)

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

[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x)  .Primitive("sin")

矩陣

矩陣是一個二維矩形數(shù)據(jù)集。它可以使用一個向量輸入到矩陣函數(shù)來創(chuàng)建。

# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow=2,ncol=3,byrow = TRUE)
print(M)

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

[,1] [,2] [,3]
[1,] "a"  "a"  "b"
[2,] "c"  "b"  "a"

數(shù)組

盡管矩陣限于兩個維度,數(shù)組可以是任何數(shù)目的尺寸大小。數(shù)組函數(shù)使用它創(chuàng)建維度的所需數(shù)量的屬性-dim。在下面的例子中,我們創(chuàng)建了兩個元素數(shù)組,這是3×3矩陣。

# Create an array.
a <- array(c('green','yellow'),dim=c(3,3,2))
print(a)

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

, , 1

     [,1]     [,2]     [,3]    
[1,] "green"  "yellow" "green"
[2,] "yellow" "green"  "yellow"
[3,] "green"  "yellow" "green"

, , 2

     [,1]     [,2]     [,3]    
[1,] "yellow" "green"  "yellow"
[2,] "green"  "yellow" "green"
[3,] "yellow" "green"  "yellow"

因子

因子是使用向量創(chuàng)建的R對象。它存儲隨同該向量作為標(biāo)記元素的不同值的向量。 標(biāo)簽始終是字符,而不論它在輸入向量的是數(shù)字或字符或布爾等。它們在統(tǒng)計建模有用。

運(yùn)用 factor() 函數(shù)創(chuàng)建因子。nlevels 函數(shù)給出級別的計數(shù)。

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.
factor_apple <- factor(apple_colors)

# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))

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

[1] green  green  yellow red    red    red    yellow green
Levels: green red yellow
# applying the nlevels function we can know the number of distinct values
[1] 3 

數(shù)據(jù)幀

數(shù)據(jù)幀是表格數(shù)據(jù)對象。不像在數(shù)據(jù)幀的矩陣,每一列可以包含不同的數(shù)據(jù)的模型。第一列可以是數(shù)字,而第二列可能是字符和第三列可以是邏輯。它與向量列表的長度相等。

數(shù)據(jù)幀所使用 data.frame()函數(shù)來創(chuàng)建。

# Create the data frame.
BMI <-     data.frame(
            gender = c("Male", "Male","Female"),
            height = c(152, 171.5, 165),
            weight = c(81,93, 78),
            Age =c(42,38,26)
            )
print(BMI)

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

gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26

數(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(), // 加隨機(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)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) 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); }