【问题标题】:R data.table control number of printed digitsR data.table 控制打印位数
【发布时间】:2016-02-22 19:50:04
【问题描述】:

我想减少在 R 中调用 data.table 对象时打印的位数。我宁愿在控制台中放置更多列,而不是每个数字最多看到 8 个小数点。

标准方法options("digits" = 4)(如this question 中所建议)似乎不适用于数据表。

一个例子:

 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 (?)

不过,一般来说,option 有效,因为

 set.seed(7)
 rnorm(1)

打印2.287

【问题讨论】:

  • 无复制。它在我的控制台上打印了四个。也许你正在使用 Rstudio。
  • 它还在我的 RStudio 中打印 4 位数字。您是否重新启动了会话?
  • 我在 mac 上使用 R.app
  • 我在 Windows 上的 RStudio 中得到 4 位数字。看起来像一个 R.app 问题。 :-(

标签: r data.table


【解决方案1】:

我无法重现此行为。 这是我的控制台的输出:

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

但是:

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

还要注意print.data.table 有一个省略号参数...,它被传递给format,因此对于单个用例,您可以像这样传递digits

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

在打印data.table 时,请参阅?format 了解更多选项。

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多