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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言均值,中位數(shù)和模式
R語言均值,中位數(shù)和模式
2017-06-20
收藏

R語言均值,中位數(shù)和模式

R統(tǒng)計(jì)分析是通過用許多內(nèi)置函數(shù)來執(zhí)行的。 大多數(shù)這些函數(shù)是R基本包的一部分。這些函數(shù)需要R向量作為輸入?yún)?shù)并給出結(jié)果。

我們正在討論本章中的函數(shù)是平均數(shù),中位數(shù)和模式。

平均值

它是通過取的值的總和,并除以一個數(shù)據(jù)系列的數(shù)量計(jì)算的。

函數(shù)mean()是用來計(jì)算這在R語言

語法

用于計(jì)算平均值在 R 中的基本語法是:

mean(x, trim = 0, na.rm = FALSE, ...)

以下是所使用的參數(shù)的說明:

x 是輸入向量。

trim 用于刪除一些要素/空格從排序向量的兩端。

na.rm 用于從輸入矢量刪除丟失的值。

示例

# Create a vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.mean <- mean(x)
print(result.mean))

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

[1] 8.22

應(yīng)用修剪選項(xiàng)

當(dāng)修剪參數(shù)被提供時,在向量中的值獲得排序,然后觀察所需要的數(shù)據(jù)從計(jì)算平均丟棄。

當(dāng)trim =0.3,是從每一端的3個值將被從找到中計(jì)算刪除的意思。

在這種情況下,排序矢量為(-21,-5,2,3,4.2,7,8,12,18,54)和從向量表除去,用于計(jì)算平均值的值從(-21,-5,2)左側(cè)和從(12,18,54)右邊。

# Create a vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.mean <-  mean(x,trim=0.3)
print(result.mean

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

[1] 5.55

應(yīng)用NA選項(xiàng)

如果有缺失值,則意味著函數(shù)返回 NA。

從計(jì)算中使用 na.rm= TRUE 刪除缺失值。這意味著刪除 NA 值。

# Create a vector.
 x <- c(12,7,3,4.2,18,2,54,-21,8,-5,NA)

# Find mean.
result.mean <-  mean(x)
print(result.mean)

# Find mean dropping NA values.
result.mean <-  mean(x,na.rm=TRUE)
print(result.mean)

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

[1] NA
[1] 8.22

中位數(shù)

在一個數(shù)據(jù)串的中間最值被稱為中值。median() 函數(shù)用于在 R 中計(jì)算此值。

語法

在 R 中用于計(jì)算中位數(shù)的基本語法是:

median(x, na.rm = FALSE)

以下是所使用的參數(shù)的說明:

x 是輸入向量。

na.rm 用于從輸入矢量刪除丟失的值。

例子

# Create the vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find the median.
median.result <- median(x)
print(median.result)

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

[1] 5.6

模式

模式是一個具有最高發(fā)生次數(shù)的一組數(shù)據(jù)的值。不同于平均數(shù)和中位數(shù),模式可以同時擁有數(shù)字和字符數(shù)據(jù)。

R沒有一個標(biāo)準(zhǔn)的內(nèi)置函數(shù)來計(jì)算模式。因此,我們創(chuàng)建一個用戶函數(shù)來計(jì)算在R數(shù)據(jù)集的模式,該函數(shù)將向量作為輸入,并給出了模式的值輸出。

示例

# Create the function.
getmode <- function(v) {
    uniqv <- unique(v)
    uniqv[which.max(tabulate(match(v, uniqv)))]
}

# Create the vector with numbers.
v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

# Calculate the mode using the user function.
result <- getmode(v)
print(result)

# Create the vector with characters.
charv <- c("o","it","the","it","it")

# Calculate the mode using the user function.
result <- getmode(charv)
print(result)

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

[1] 2
[1] "it"

數(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){ //倒計(jì)時完成 $(".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); }