Ensuring long digits are maintained in CSV output

若如初见. 提交于 2019-12-10 18:03:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!