2

I would like to reduce the number of digits printed when calling a data.table object in R. I'd rather fit more columns into the console than seeing up to 8 decimal points for each number.

The standard way, options("digits" = 4) (as suggested in this question), does not seem to work with data tables.

An example:

 set.seed(7)
 DT <- data.table(x = rnorm(10), y = 1:10)
 DT # prints 10 digits for me, including the decimal point

 options("digits" = 4)
 DT # prints 8 digits (?)

Generally, though, option works, since

 set.seed(7)
 rnorm(1)

prints 2.287

Community
  • 1
  • 1
altabq
  • 1,322
  • 1
  • 20
  • 33

1 Answers1

1

I cannot reproduce this behavior. Here is the output of my console:

set.seed(7)
DT <- data.table(x = rnorm(10), y = 1:10)
DT

           x  y
1:  2.28724716134  1
2: -1.19677168222  2
3: -0.69429251044  3
4: -0.41229295114  4
5: -0.97067334112  5
6: -0.94727994523  6
7:  0.74813934029  7
8: -0.11695522589  8
9:  0.15265762628  9
10: 2.18997810733 10

However:

options("digits" = 4)

DT
      x  y
 1:  2.2872  1
 2: -1.1968  2
 3: -0.6943  3
 4: -0.4123  4
 5: -0.9707  5
 6: -0.9473  6
 7:  0.7481  7
 8: -0.1170  8
 9:  0.1527  9
 10: 2.1900 10

Note also that print.data.table has an ellipsis argument ... which is passed on to format, so for a single use case you can pass digits along like so:

print.data.table(DT, digits=4)

See ?format for additional options when printing a data.table.

dardisco
  • 5,086
  • 2
  • 39
  • 54
amonk
  • 1,769
  • 2
  • 18
  • 27