如何使小标题显示有效数字

时间:2018-07-31 20:24:18

标签: r tibble

我正在处理具有有效数字(即“。”之后的数字)的数据。在将我的数据作为基础R中的变量查看数据以及将数据存储在数据框中时,这些数字也会出现。但是,当我查看数据时它们不会出现。

我需要查看这些有效数字以进行工作。有没有办法让它们在使用小标题时出现?

以下是可重现的示例:

x有5个有效数字,使用底数R时显示3:

x = 1234.56789
x
[1] 1234.568

在数据框内,还将显示3个有效数字:

df = data.frame(x=x)
df
         x
1 1234.568

在小标题内,将显示0个有效数字:

library(tibble)
df = tibble(x=x)
df
# A tibble: 1 x 1
      x
  <dbl>
1 1235.

再次,我正在寻找一种方法,使在查看小标题数据时出现0个以上有效数字。

这是我的sessionInfo()的结果:

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] tibble_1.4.2      readr_1.1.1       choroplethr_3.6.2
[4] acs_2.1.3         XML_3.98-1.12     stringr_1.3.1    

loaded via a namespace (and not attached):
 [1] httr_1.3.1          maps_3.3.0          splines_3.5.1      
 [4] Formula_1.2-3       assertthat_0.2.0    sp_1.3-1           
 [7] latticeExtra_0.6-28 yaml_2.2.0          pillar_1.3.0       
[10] backports_1.1.2     lattice_0.20-35     glue_1.3.0         
[13] uuid_0.1-2          digest_0.6.15       RColorBrewer_1.1-2 
[16] checkmate_1.8.5     colorspace_1.3-2    htmltools_0.3.6    
[19] Matrix_1.2-14       plyr_1.8.4          pkgconfig_2.0.1    
[22] WDI_2.5             purrr_0.2.5         scales_0.5.0       
[25] jpeg_0.1-8          tigris_0.7          ggmap_2.6.1        
[28] htmlTable_1.12      ggplot2_3.0.0       nnet_7.3-12        
[31] lazyeval_0.2.1      cli_1.0.0           proto_1.0.0        
[34] survival_2.42-6     RJSONIO_1.3-0       magrittr_1.5       
[37] crayon_1.3.4        maptools_0.9-2      fansi_0.2.3        
[40] foreign_0.8-71      class_7.3-14        tools_3.5.1        
[43] data.table_1.11.4   hms_0.4.2           geosphere_1.5-7    
[46] RgoogleMaps_1.4.2   munsell_0.5.0       cluster_2.0.7-1    
[49] bindrcpp_0.2.2      compiler_3.5.1      e1071_1.7-0        
[52] rlang_0.2.1         classInt_0.2-3      units_0.6-0        
[55] grid_3.5.1          rstudioapi_0.7      rjson_0.2.20       
[58] rappdirs_0.3.1      htmlwidgets_1.2     base64enc_0.1-3    
[61] gtable_0.2.0        curl_3.2            DBI_1.0.0          
[64] reshape2_1.4.3      R6_2.2.2            gridExtra_2.3      
[67] knitr_1.20          dplyr_0.7.6         rgdal_1.3-3        
[70] utf8_1.1.4          bindr_0.1.1         Hmisc_4.1-1        
[73] stringi_1.2.4       Rcpp_0.12.18        mapproj_1.2.6      
[76] sf_0.6-3            rpart_4.1-13        acepack_1.4.1      
[79] png_0.1-7           spData_0.2.9.0      tidyselect_0.2.4   

1 个答案:

答案 0 :(得分:3)

您可以设置选项pillar.sigfig

options(pillar.sigfig = 1)
as_tibble(iris)
# # A tibble: 150 x 5
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#  1           5.          4.           1.         0.2 setosa 
#  2           5.          3            1.         0.2 setosa 
#  3           5.          3.           1.         0.2 setosa 
#  4           5.          3.           2.         0.2 setosa 
#  5           5           4.           1.         0.2 setosa 
#  6           5.          4.           2.         0.4 setosa 
#  7           5.          3.           1.         0.3 setosa 
#  8           5           3.           2.         0.2 setosa 
#  9           4.          3.           1.         0.2 setosa 
# 10           5.          3.           2.         0.1 setosa 


options(pillar.sigfig = 7)
tb = tibble(x=x)
tb
# # A tibble: 1 x 1
#            x
#        <dbl>
#   1 1234.568

另请参阅:

?`tibble-options`

或在线:

https://www.rdocumentation.org/packages/tibble/versions/1.4.2/topics/tibble-options