亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

R語(yǔ)言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式

 更新時(shí)間:2021年04月19日 09:08:52   作者:datanewlook  
這篇文章主要介紹了R語(yǔ)言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

控制R語(yǔ)言科學(xué)計(jì)算法顯示有兩個(gè)option: digitis和scipen。介紹的資料很少,而且有些是錯(cuò)誤的。經(jīng)過翻看R語(yǔ)言的幫助和做例子仔細(xì)琢磨,總結(jié)如下:

默認(rèn)的設(shè)置是:

getOption("digits")
[1] 7
getOption("scipen")
[1] 0

digits 有效數(shù)字字符的個(gè)數(shù),默認(rèn)是7, 范圍是[1,22]

scipen 科學(xué)計(jì)數(shù)顯示的penalty,可以為正為負(fù),默認(rèn)是0

R輸出數(shù)字時(shí),使用普通數(shù)字表示的長(zhǎng)度 <= 科學(xué)計(jì)數(shù)法表示的字符長(zhǎng)度 + scipen長(zhǎng)度時(shí),保留普通數(shù)字表示的長(zhǎng)度,否者采用科學(xué)計(jì)數(shù)法表示。

舉個(gè)栗子:

> options(digits = 2) # 有效數(shù)字為2位
> options(scipen = 1)
> 1         # 1e+00 長(zhǎng)度為5, 保留1顯示,長(zhǎng)度為1
[1] 1
> 12345678   # 1.2e+07, 長(zhǎng)度為7, 7 + scipen = 8, 普通數(shù)字表示長(zhǎng)度為8, 沒有超過8, 任然保留不同數(shù)字的表示。
[1] 12345678
> 123456789   # 1.2e+08, 長(zhǎng)度為7, 7 + scipen =8, 普通數(shù)字表示長(zhǎng)度為9,因此切換成科學(xué)計(jì)數(shù)法表示
[1] 1.2e+08

一個(gè)簡(jiǎn)單的方法(不那么準(zhǔn)確,比如digits=1時(shí),沒有小數(shù)點(diǎn);數(shù)非常大時(shí),指數(shù)可能是3位數(shù))估算最長(zhǎng)的數(shù)字串可以這樣:

digits + 1 (小數(shù)點(diǎn))+ 4 (e+XX科學(xué)計(jì)數(shù)法表示) + scipen

比如剛才最長(zhǎng)不用科學(xué)計(jì)數(shù)法表示的數(shù)字長(zhǎng)度是2+1+4+1 = 8

我們看看修改scipen = -2, 驗(yàn)證是不是最長(zhǎng)數(shù)字長(zhǎng)度是2+1+4 - 2 = 5

> options(scipen = -2)
> 1234
[1] 1234
> 12345
[1] 12345
> 123456
[1] 1.2e+05

果然!

補(bǔ)充:R語(yǔ)言設(shè)置數(shù)值輸出(保留至小數(shù)點(diǎn)后位數(shù)和保留有效數(shù)字)

在R語(yǔ)言中,數(shù)字的輸出默認(rèn)為7位:

> a = 0.1234567890   #10位
> a
[1] 0.1234568

注:輸出結(jié)果四舍五入。

1 options(digits)函數(shù)

通過options(digits)函數(shù)設(shè)置輸出長(zhǎng)度,當(dāng)digits = 3時(shí):

> options(digits = 3) 
> a = 0.1234567890 #10位
> a
[1] 0.123

當(dāng)digits = 10時(shí):

> options(digits = 10)
> a = 0.1234567890   #10位
> a
[1] 0.123456789

digits最大取22,超過22會(huì)報(bào)錯(cuò):

> options(digits = 3)
> options(digits = 22)
> options(digits = 23)
Error in options(digits = 23) : 
  invalid 'digits' parameter, allowed 0...22

輸出的結(jié)果只保留了9位,末尾的0被省略。

2 round(x, n)函數(shù)

round(x, n)函數(shù)中,x為數(shù)字,n為小數(shù)點(diǎn)后保留的位數(shù),設(shè)置n = 4時(shí):

> a = 0.1234567890   #10位
> round(a, 4)
[1] 0.1235
> a = 1.234567890   #小數(shù)點(diǎn)后9位
> round(a, 4)
[1] 1.2346

注:輸出結(jié)果四舍五入。

當(dāng)設(shè)置n = 10時(shí):

> a = 0.1234567890   #10位
> round(a, 10)
[1] 0.123456789

輸出的結(jié)果只保留了9位,末尾的0被省略。

當(dāng)小數(shù)點(diǎn)后的0的位數(shù)超過n時(shí),輸出的結(jié)果為0:

> a = 0.0001234567890   #13位
> round(a, 3)
[1] 0
> a = 0.0001234567890   #13位
> round(a, 4)
[1] 1e-04

3 signif(y, n)函數(shù)

signif(x, n)函數(shù)中,x為數(shù)字,n為有效數(shù)字的個(gè)數(shù) ,當(dāng)n = 4時(shí):

> a = 1.234567890   #小數(shù)點(diǎn)后9位
> signif(a, 4)
[1] 1.235
> a = 0.000001234567890   #小數(shù)點(diǎn)后15位
> signif(a, 4)
[1] 1.235e-06

當(dāng)n = 10時(shí):

> a = 1.234567890   #小數(shù)點(diǎn)后9位
> signif(a, 10)
[1] 1.23456789

此時(shí)數(shù)字末尾的0依舊被省略。

4 sprintf(fmt, …)函數(shù)

> a = 0.1234567890   #小數(shù)點(diǎn)后10位
> sprintf("%0.4f", a)
[1] "0.1235"
> a = 0.1234567890   #小數(shù)點(diǎn)后10位
> sprintf("%0.10f", a)
[1] "0.1234567890"

通過sprintf(fmt, ...)函數(shù)可以保留末尾的0。

當(dāng)輸入為整數(shù)時(shí),位數(shù)不夠會(huì)在輸入值前面補(bǔ)0:

> a = 12456789
> sprintf("%03d", a)
[1] "12456789"
> a = 12
> sprintf("%03d", a)
[1] "012"

歡迎大家批評(píng)指正。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論