问题
I have an R data frame which needs to be saved as a csv file however one numeric column changes its format when saved as a csv file.
I have a character column named ID as below:
df <- data.frame(ID = as.character(1181050000000002, 1189050000000003),
Other = c("John", "Mary"))
When I save as a csv file on r, it changes to
write.csv(df, "df.csv", row.names = FALSE)
df <- read.csv("df.csv")
df$ID
ID
1181050000000000
1189050000000000
The output when I open the csv file should appear as original
ID
1181050000000002
1189050000000003
回答1:
Here's an approach that worked for me using readr
:
library(readr)
write_csv(df, "df.csv")
options(scipen=999)
df1 <- read.csv("df.csv")
df1
# ID
#1 1181050000000002
#2 1189050000000003
And here's a screen shot:
回答2:
Try library bit64
dt$ID <- as.integer64(dt$id)
It is a high precision integer. Usually works well with this long number issues (if all your id are combinations of digits, of course)
来源:https://stackoverflow.com/questions/54647922/ensuring-long-digits-are-maintained-in-csv-output