如何强制R中的View不使用科学记数法并限制数字?

时间:2015-03-23 04:28:25

标签: r dataframe

我在R中使用View来显示数据组。我使用以下语句创建视图显示:

options(scipen = 600)
View(format.data.frame(port.frame, digits = 3, nsmall = 2, 
                       justify = "centre"))

    Names   Close   AnnReturn   SD  EqWgt   MeanVar MomWgt  NaivPar
1      LMOIX        30.50   14.57   16.59    0.250  -0.00000000000000000643  0.480   0.221
2      TREPX        11.55    9.58   15.65    0.250   0.28611506417112164691  0.240   0.235
3      TRPGX        12.64   15.95   13.14    0.250   0.71388493582887835309  0.160   0.280
4      SMVTX        14.00   15.74   13.91    0.250   0.00000000000000000347  0.120   0.264
5   Sum weights      0.00    0.00    0.00    1.000   1.00000000000000000000  1.000   1.000
6    Port. Ret.      0.00    0.00    0.00   13.960  14.12857148500626180976 13.732  14.094
7    Port. Vol.      0.00    0.00    0.00   13.803  13.23194704976875080149 14.431  13.720
8   Sharpe Rat.      0.00    0.00    0.00    0.729   0.79632953321575861150  0.679   0.745
9   Sortino Rat.     0.00    0.00    0.00    1.134   1.20134743486126471801  1.067   1.153
10   Info. Rat.      0.00    0.00    0.00    0.683   0.69660638606011093810  0.664   0.690
11      Beta         0.00    0.00    0.00    0.260   0.24611926403332246016  0.264   0.258

可以看出,第6列忽略了我对3位数的请求,如果我不使用“scipen”表达式,它会对该列使用科学记数法。我希望每列有3位数,2位小数点后2位数。为什么它在第6列中忽略了我的小数点请求?

2 个答案:

答案 0 :(得分:0)

您可以使用round来强制解决问题(如果需要,可以在临时数据框中):

dat[,2:8] <- lapply(dat[,2:8], round, 2)
format(dat, justify = "centre") # or View(format(dat, justify = "centre"))

<强>结果

          Names Close AnnReturn    SD EqWgt MeanVar MomWgt NaivPar
1     LMOIX     30.50     14.57 16.59  0.25    0.00   0.48    0.22
2     TREPX     11.55      9.58 15.65  0.25    0.29   0.24    0.24
3     TRPGX     12.64     15.95 13.14  0.25    0.71   0.16    0.28
4     SMVTX     14.00     15.74 13.91  0.25    0.00   0.12    0.26
5  Sum weights   0.00      0.00  0.00  1.00    1.00   1.00    1.00
6   Port. Ret.   0.00      0.00  0.00 13.96   14.13  13.73   14.09
7   Port. Vol.   0.00      0.00  0.00 13.80   13.23  14.43   13.72
8  Sharpe Rat.   0.00      0.00  0.00  0.73    0.80   0.68    0.74
9  Sortino Rat.  0.00      0.00  0.00  1.13    1.20   1.07    1.15
10  Info. Rat.   0.00      0.00  0.00  0.68    0.70   0.66    0.69
11     Beta      0.00      0.00  0.00  0.26    0.25   0.26    0.26

数据

dat <- read.csv(text="Names,Close,AnnReturn,SD,EqWgt,MeanVar,MomWgt,NaivPar
LMOIX,30.50,14.57,16.59,0.250,-0.00000000000000000643,0.480,0.221
TREPX,11.55,9.58,15.65,0.250,0.28611506417112164691,0.240,0.235
TRPGX,12.64,15.95,13.14,0.250,0.71388493582887835309,0.160,0.280
SMVTX,14.00,15.74,13.91,0.250,0.00000000000000000347,0.120,0.264
Sum weights,0.00,0.00,0.00,1.000,1.00000000000000000000,1.000,1.000
Port. Ret.,0.00,0.00,0.00,13.960,14.12857148500626180976,13.732,14.094
Port. Vol.,0.00,0.00,0.00,13.803,13.23194704976875080149,14.431,13.720
Sharpe Rat.,0.00,0.00,0.00,0.729,0.79632953321575861150,0.679,0.745
Sortino Rat.,0.00,0.00,0.00,1.134,1.20134743486126471801,1.067,1.153
Info. Rat.,0.00,0.00,0.00,0.683,0.69660638606011093810,0.664,0.690
Beta,0.00,0.00,0.00,0.260,0.24611926403332246016,0.264,0.258")

答案 1 :(得分:0)

您可以使用lucid包。从包装小插图:

  

该软件包提供了一种漂亮打印浮点数矢量的方法,......

library(lucid)

使用内置数据集mtcars

的示例
lucid(mtcars[15:20,])

                     mpg cyl  disp  hp drat   wt qsec vs am gear carb
Cadillac Fleetwood  10.4   8 472   205 2.93 5.25 18    0  0    3    4
Lincoln Continental 10.4   8 460   215 3    5.42 17.8  0  0    3    4
Chrysler Imperial   14.7   8 440   230 3.23 5.34 17.4  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.2  19.5  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.62 18.5  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.84 19.9  1  1    4    1

它也适用于View()命令。