preserve old (pre 3.1.0) type.convert behavior

你说的曾经没有我的故事 提交于 2019-11-30 17:28:32

In version 3.1.1, there is this change listed in the News file:

type.convert(), read.table() and similar read.*() functions get a new numerals argument, specifying how numeric input is converted when its conversion to double precision loses accuracy. The default numerals = "allow.loss" allows accuracy loss, as in R versions before 3.1.0.

Much of post-release discussion about the original change, including the decisions to revert the default behavior with an additional warning, can be found in a thread on the developers' email list.

For version 3.1.0, code will have to be modified to get the old behavior. Switching to 3.1.1 is another strategy.

The mention of this change for version 3.1.0 (from the same News file) says

type.convert() (and hence by default read.table()) returns a character vector or factor when representing a numeric input as a double would lose accuracy. Similarly for complex inputs.

If a file contains numeric data with unrepresentable numbers of decimal places that are intended to be read as numeric, specify colClasses in read.table() to be "numeric".

Note: original answer was written when the applicable version with the fix was 3.1.0 patched. The answer has been updated now that 3.1.1 has been released.

Try using data.table's fread:

# create test data set "a.dat"
Lines <- "num1 num2\n1.1 1.1234567890123456\n2.2 2.2\n3.3 3.3\n"
cat(Lines, file = "a.dat")

#####

library(data.table)

DT <- fread("a.dat")
str(DT)
## Classes ‘data.table’ and 'data.frame':  3 obs. of  2 variables:
## $ num1: num  1.1 2.2 3.3
## $ num2: num  1.12 2.2 3.3
## - attr(*, ".internal.selfref")=<externalptr> 

class(DT)
## [1] "data.table" "data.frame"

DF <- as.data.frame(DT) 
class(DF)
## [1] "data.frame"

ADDED LATER Since this answer was posted the latest patched version of R 3.1.0 has come out and by default reverts back to the old behavior with a new numerals argument to specify it differently. See type.convert and read.table

Since I don't have rep to comment on Brian Diggs's response - for future reference, the new argument is now called "numerals" (not "exact"). From http://cran.r-project.org/bin/windows/base/NEWS.R-3.1.0patched.html:

type.convert(), read.table() and similar read.*() functions get a new numerals argument, specifying how numeric input is converted when its conversion to double precision loses accuracy. The default numerals = "allow.loss" allows accuracy loss, as in R versions before 3.1.0.

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