write.csv更改R中的数据

时间:2014-12-18 04:09:09

标签: r csv

我正在尝试使用write.csv,但它给了我错误的答案,我做错了什么,我该怎么做才能保留它?

示例数据:

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

1 个答案:

答案 0 :(得分:1)

我的猜测是你用Excel和Excel打开了用科学计数法显示的csv文件。正如@Ananda所说,Excel可能会以不同的方式阅读大数字。也许你可以尝试write.table功能:

# 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 spreadsheets are hard to get right