write.csv更改R中的数据 [英] write.csv changes the data in R

查看:341
本文介绍了write.csv更改R中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 write.csv 但它给我错误的答案,我做错了,我该怎么做,以保留?

I am trying to use write.csv but it give me wrong answer,what am I doing wrong, what should I do to preserve that ?

示例数据:

df1 <- structure(list(id = c(10,5788,6877,35814,10,3637,3751,21106,21144,21409,24765,
                       34988,6,904,2014,26509,2924,34034,35927)                     
                  , book_id = c("1427542124131848","1427542124131848","1427542124131848","1427542124131848",
                                "677165348994231","677165348994231","677165348994231","677165348994231",
                                "677165348994231","677165348994231","677165348994231","677165348994231",
                                "74117771667","74117771667","74117771667","74117771667","74117771667","74117771667","74117771667"),
                   class = "data.frame")

代码:

Temp=as.data.table(df1)
t2=Temp[, list(ID = paste(id, collapse = ","), 
           numofid = length(id)), by = "book_id"]

 #        book_id                                         ID numofid
#1 1427542124131848                         10,5788,6877,35814       4
#2  677165348994231 10,3637,3751,21106,21144,21409,24765,34988       8
#3      74117771667          6,904,2014,26509,2924,34034,35927       7

write.csv(t2  , file = "t2.csv",row.names=FALSE)

csv文件:

 #1.42754E+15   105,788,687,735,814 4
 #6.77165E+14   10,363,737,512,110,600,000,000,000,000,000,000  8
 #74117771667   690,420,142,650,929,000,000,000,000 7


推荐答案

我的猜测是,你在Excel和Excel中以科学记数法打开了csv文件。正如@Ananda认为的,Excel可以不同地读取大数字。可能你可以尝试 write.table 函数:

My guess is that you opened the csv file in Excel and Excel displayed in scientific notation. And as @Ananda opined, Excel could be reading the big number differently. May be you could try write.table function:

# the t2.txt file will be saved to your R working directory
write.table(t2, sep="\t", file="t2.txt", row.names=FALSE)

Excel是臭名昭着的无法获得数字。要分享的一篇文章: Excel电子表格很难正确

Excel is notorious for not being able to get numbers right. One article to share: Excel spreadsheets are hard to get right

这篇关于write.csv更改R中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆