在 R 中没有正确读取小数

decimals not being read properly in R

我正在尝试获取整数 193525.0768 但它的小数点被删除了 (?)。请给我解释一下。

df <- tibble(
  x = "193525.0768"
) %>% 
  mutate(x = as.numeric(x))

print(df, digits = 10) # decimals removed. I expect it to maintain the decimals numbers

# A tibble: 1 x 1
        x
    <dbl>
1 193525.

df[1,1][[1]] # decimals removed

# 193525

x <- "193525.0768"
print(as.numeric(x), digits = 10) # decimals not removed
# 193525.0768

您遇到的是打印问题,而不是 reading-in 问题。 tibble print 方法不接受数字参数 - 详情请参阅 ?print.tbl。您可以使用 print.data.frame 显式绕过 tibble print 方法并使用 data.frame print 方法代替,该方法确实需要一个 digits 参数:

tibble(x = "193525.0768") %>% 
  mutate(x = as.numeric(x)) %>% 
  print.data.frame(digits = 10)
#             x
# 1 193525.0768

或者您可以使用 pillar.sigfig 选项更改默认值(在 ?print.tbl 中提到)。默认值为 3 - 这令人困惑,因为如果我按字面意思理解,我希望 193525.0768 打印为 194000 ... pillar 包中可能有解释推理的文档。

options(pillar.sigfig = 10)

tibble(x = "193525.0768") %>%
  mutate(x = as.numeric(x))
#             x
# 1 193525.0768

或者,使用数据框而不是小标题:

data.frame(x = "193525.0768") %>% 
  mutate(x = as.numeric(x)) %>% 
  print(digits = 10)
#             x
# 1 193525.0768