read_excel 截断数字,尽管设置了数字选项

read_excel truncates numbers in spite of setting digits option

我正在尝试使用 "readxl" 包中的 read_excel() 函数将 excel 文件读入 R。 excel 文件中单元格中的数字有 3 位小数,例如 0,684 1,338 74,296。在 R 数据框中,相同的数字是 0,684 1,34 74,3。 为什么只剩下 3 位数字?

我设置了 options(digits=5)(也尝试了不同的值)。

我也尝试过 read_excel("file.xlsx", col_types = c("text", "text", "text"))(它给了我字符串:74.296000000000006 1.3380000000000001 0.68400000000000005),然后用 as.numeric() 转换为数字,但数字又被截断了:0,684 1,34 74,3.

如果你知道原因请告诉我:)

注意不要混淆 "how many digits are there" 和 "how many digits R is showing you"。不看你的代码就很难确定,但我怀疑你在打印小标题,像这样:

library(readxl)
my_junk <- read_excel("~/Downloads/test.xlsx")
print(my_junk)
#> # A tibble: 3 x 1
#>    test
#>   <dbl>
#> 1 0.684
#> 2 1.000
#> 3 1.00

tibbles 的默认打印方法仅显示几个有效数字。而且它似乎没有响应 options 中的数字设置。但是,如果我们将其转换为 data.frame,您会看到数字一直都在那里:

library(dplyr)
my_junk %>%
  data.frame %>%
  print( digits = 7)  
#>       test
#> 1 0.684000
#> 2 0.999950
#> 3 1.000001

所以您的数字可能在那里,只是没有显示。