3

options(scipen = 200)
options(digits = 2)
stat.desc(train_cont)
I have seen people use pastecs as an alternative for summary for summarize data. What is the use of scipen and digits. Please help me understand as I am new to this.

1 Answers1

3

I think the documentation is decent, but perhaps a demonstration would help to show how different combinations of each affect the outcome.

Helpers:

func <- function(dig, sci, val = pi) {
  mapply(function(d,s)
    withr::with_options(list(digits=d, scipen=s),
                        gsub("^\\[1\\]\\s*", "", capture.output(print(val)))),
    dig, sci)
}
digs <- array(digs <- c(1, 2, 5, 10), dim = 4, dimnames = list(digits = digs))
scis <- array(scis <- c(-10, -1, 0, 1, 10), dim = 5, dimnames = list(scipen = scis))

Basic output of with pi:

outer(digs, scis, FUN = func, val = pi)
#       scipen
# digits -10               -1            0             1             10           
#     1  "3e+00"           "3"           "3"           "3"           "3"          
#     2  "3.1e+00"         "3.1"         "3.1"         "3.1"         "3.1"        
#     5  "3.1416e+00"      "3.1416"      "3.1416"      "3.1416"      "3.1416"     
#     10 "3.141592654e+00" "3.141592654" "3.141592654" "3.141592654" "3.141592654"

Where interpretation is along the lines of:

                        "Prefer fewer significant digits"   
#       scipen           ^                      
# digits 10              |                      
#     1  "3"             |                      
#     2  "3.1"              |                   
#     5  "3.1416"           |                   
#     10 "3.141592654"      v                   
                            "Prefer more significant digits"

         <--- "I like sci-notation"                 "I despise sci-notation" ---->
#       scipen
# digits -10               -1            0             1             10           
#     10 "3.141592654e+00" "3.141592654" "3.141592654" "3.141592654" "3.141592654"

Let's up the ante a little by using 1e6 * pi, really demonstrating how scipen changes things:

outer(digs, scis, FUN = func, val = 1e6 * pi)
#       scipen
# digits -10               -1            0             1             10           
#     1  "3e+06"           "3e+06"       "3e+06"       "3e+06"       "3141593"    
#     2  "3.1e+06"         "3.1e+06"     "3141593"     "3141593"     "3141593"    
#     5  "3.1416e+06"      "3141593"     "3141593"     "3141593"     "3141593"    
#     10 "3.141592654e+06" "3141592.654" "3141592.654" "3141592.654" "3141592.654"

And now extremely uninteresting examples of 1e12 and 1e-12:

outer(digs, scis, FUN = func, val = 1e12)
#       scipen
# digits -10     -1      0       1       10             
#     1  "1e+12" "1e+12" "1e+12" "1e+12" "1000000000000"
#     2  "1e+12" "1e+12" "1e+12" "1e+12" "1000000000000"
#     5  "1e+12" "1e+12" "1e+12" "1e+12" "1000000000000"
#     10 "1e+12" "1e+12" "1e+12" "1e+12" "1000000000000"

outer(digs, scis, FUN = func, val = 1e-12)
#       scipen
# digits -10     -1      0       1       10              
#     1  "1e-12" "1e-12" "1e-12" "1e-12" "0.000000000001"
#     2  "1e-12" "1e-12" "1e-12" "1e-12" "0.000000000001"
#     5  "1e-12" "1e-12" "1e-12" "1e-12" "0.000000000001"
#     10 "1e-12" "1e-12" "1e-12" "1e-12" "0.000000000001"

The frontier-lines are different based on the number being shown: for pi, a scipen above -1 doesn't change, but that is different for 1e12 and 1e-12 ("significant digits" comes to mind, where pi has many non-zero digits).

One of the biggest bottom lines: this is all about aesthetics, completely about how things display on the R console or in generated reports. At no time does the value of pi change, just how it is displayed. This also affects timestamps, using options("digits.secs").

BTW: I also tested scipen=999 (and negative), and they were no different from +/- 10.

r2evans
  • 141,215
  • 6
  • 77
  • 149