You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

readr包write_csv存CSV时末尾零触发科学计数法问题咨询

Why does readr::write_csv() save some numbers in scientific notation, and how to fix it?

First, let’s break down your questions and solutions clearly:

Is this behavior expected?

Yes, this is the default behavior of write_csv() in readr 1.1.1 (the version you’re using). The function prioritizes compact file output for numeric values, so when an integer ends with multiple trailing zeros, it switches to scientific notation (like 4100738e3 instead of 4100738000). While this was intentional for efficiency, it creates compatibility issues with tools like Excel, which often misinterpret scientific notation in CSV files—especially when these values are meant to be treated as identifiers (not mathematical numbers).

Alternative solutions (other than converting to character)

You have a few reliable options to avoid scientific notation without changing your numeric vector to character:

1. Use write_excel_csv() (best for Excel compatibility)

Readr includes a dedicated function optimized for Excel. write_excel_csv() automatically skips scientific notation for integers and formats values in a way Excel interprets correctly:

library(readr)
x <- c(4100750938, 4104806156, 4001682199, 4100738000)
df <- data.frame(x)
write_excel_csv(df, "df_excel.csv")

This will write all your values as full integers (e.g., 4100738000) with no scientific notation.

2. Upgrade readr and use the num_extra parameter

If you can upgrade your readr package to version 1.3.0 or later, you can use the num_extra argument to force the function to output all digits of numeric values:

# Requires readr >= 1.3.0
library(readr)
x <- c(4100750938, 4104806156, 4001682199, 4100738000)
df <- data.frame(x)
write_csv(df, "df.csv", num_extra = Inf)

Setting num_extra = Inf tells write_csv() to never truncate or abbreviate numeric values with scientific notation.

3. Use base R's write.csv()

Base R’s built-in write.csv() function doesn’t use scientific notation for integers by default (as long as values fit within integer limits). Just disable row names to match readr::write_csv()’s behavior:

x <- c(4100750938, 4104806156, 4001682199, 4100738000)
df <- data.frame(x)
write.csv(df, "df_base.csv", row.names = FALSE)

This will write full integer values without scientific notation.


内容的提问来源于stack exchange,提问作者gplngr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:59:02