Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HughParsonage committed Jan 16, 2018
1 parent 1154851 commit a4b6c35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/fread.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fread <- function(input="",file,sep="auto",sep2="auto",dec=".",quote="\"",nrows=
isTrueFalse = function(x) isTRUE(x) || identical(FALSE, x)
isTrueFalseNA = function(x) isTRUE(x) || identical(FALSE, x) || identical(NA, x)
stopifnot( isTrueFalse(strip.white), isTrueFalse(blank.lines.skip), isTrueFalse(fill), isTrueFalse(showProgress),
isTrueFalse(stringsAsFactors), isTrueFalse(verbose), isTrueFalse(check.names), isTrueFalse(logical01) )
isTrueFalse(stringsAsFactors) || is.double(stringsAsFactors), isTrueFalse(verbose), isTrueFalse(check.names), isTrueFalse(logical01) )
stopifnot( is.numeric(nrows), length(nrows)==1L )
if (is.na(nrows) || nrows<0) nrows=Inf # accept -1 to mean Inf, as read.table does
if (identical(header,"auto")) header=NA
Expand Down Expand Up @@ -127,7 +127,7 @@ fread <- function(input="",file,sep="auto",sep2="auto",dec=".",quote="\"",nrows=
if (stringsAsFactors) {
# Re Issue 2025
if (is.double(stringsAsFactors)) {
should_be_factor <- function(v) is.character(v) && uniqueN(v) < nr / stringsAsFactors
should_be_factor <- function(v) is.character(v) && uniqueN(v) < nr * stringsAsFactors
cols_to_factor <- which(vapply(ans, should_be_factor, logical(1L)))
if (verbose) {
cat("stringsAsFactors=", stringsAsFactors, ", interpreting as the minimum number",
Expand Down
9 changes: 9 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -10181,6 +10181,15 @@ test(1743.307, names(fread(data1743.307, colClasses=list(NULL = c("C", "D"), NUL
test(1743.31, names(fread(data1743, colClasses="NULL")), LETTERS[1:4], warning="colClasses.*quoted.*interpreted as colClasses.*NULL")
test(1743.32, sapply(fread("A,B\na,0+1i", colClasses="complex"), class), c(A="character", B="complex"), warning="Column 1 was set by colClasses to be .complex")

## stringsAsFactors = double Issue #2025
fwrite(data.table(v1 = sample(letters, size = 26 * 10, replace = TRUE), V2 = paste0("A", c(1:259, 1)), V3 = sample.int(26, n = 260, replace = TRUE)), temp1743_41.csv <- tempfile(fileext = ".csv"))
test(1743.411, sapply(fread(temp1743_41.csv), class)[[1]], "character")
test(1743.412, sapply(fread(temp1743_41.csv, stringsAsFactors = 0), class)[[1]], "character")
test(1743.413, sapply(fread(temp1743_41.csv, stringsAsFactors = 0.10), class)[[1]], "character")
test(1743.414, sapply(fread(temp1743_41.csv, stringsAsFactors = 0.11), class)[[1]], "factor")
test(1743.415, sapply(fread(temp1743_41.csv, stringsAsFactors = 0.12), class)[[1]], "factor")
test(1743.416, sapply(fread(temp1743_41.csv, stringsAsFactors = 1.00), class)[[2]], "factor")
invisible(file.remove(temp1743_41.csv))

# rolling join stopped working for double with fractions, #1904
DT = data.table(A=c(1999.917,2000.417,2000.917,2001.417,2001.917))
Expand Down

0 comments on commit a4b6c35

Please sign in to comment.