New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length one character vector always guessed as character #333

Closed
grandtiger opened this Issue Dec 10, 2015 · 3 comments

Comments

Projects
None yet
4 participants
@grandtiger

grandtiger commented Dec 10, 2015

I have trouble attaching files, so I just put the content of the files here since they are simple.

I found a surprising behavior using read_csv function in readr package. The format of date is different when the input file has only one row vs. multiple rows.

Could you please take a look and see if this is a bug or user error?

Thanks a lot!

file1.csv

"COL11","COL21","12/09/15"

file2.csv

"COL11","COL21","12/09/15"
"COL12","COL22","12/10/15"
"COL13","COL23","12/11/15"

readr_test.R

library("readr")
default.locale <- locale(date_names = "en", date_format = "%m/%d/%y",
                         time_format = "%H:%M:%S", decimal_mark = ".", grouping_mark = NULL,
                         tz = "UTC", encoding = "UTF-8", asciify = FALSE)
file1 <- read_csv('file1.csv', locale = default.locale, col_names = FALSE)
colnames(file1) <- c("COL1","COL2","DATE")
View(file1)
class(file1$DATE)
file2 <- read_csv('file2.csv', locale = default.locale, col_names = FALSE)
colnames(file2) <- c("COL1","COL2","DATE")
View(file2)
class(file2$DATE)
print(class(file1$DATE) == class(file2$DATE))
@jennybc

This comment has been minimized.

Member

jennybc commented Feb 2, 2016

I tried to figure out if this is related to a problem I'm having. It is not.

But since I have it, here's the example above, stripped way down:

library("readr")
(a <- read_csv("2015-12-09\n", col_names = FALSE))
#> Source: local data frame [1 x 1]
#> 
#>           X1
#>        (chr)
#> 1 2015-12-09
(b <- read_csv("2015-12-09\n2015-12-10", col_names = FALSE))
#> Source: local data frame [2 x 1]
#> 
#>           X1
#>       (date)
#> 1 2015-12-09
#> 2 2015-12-10
class(a$X1)
#> [1] "character"
class(b$X1)
#> [1] "Date"
@hadley

This comment has been minimized.

Member

hadley commented Jun 2, 2016

Even more minimal:

read_csv("1\n", col_names = FALSE)
#> Source: local data frame [1 x 1]
#> 
#>      X1
#>   <chr>
#> 1     1

Likely to be an off-by-one error

@hadley hadley added bug ready labels Jun 2, 2016

@hadley hadley changed the title from read_csv handles one line vs. multiple lines differently to Length one character vector always guessed as character Jun 2, 2016

@hadley hadley added the collector label Jun 2, 2016

@jimhester jimhester added in progress and removed ready labels Jun 7, 2016

@jimhester jimhester self-assigned this Jun 7, 2016

@jimhester

This comment has been minimized.

Member

jimhester commented Jun 7, 2016

After bisecting 0cb2d0b is the commit that introduced this bug

@hadley hadley closed this in #417 Jun 7, 2016

@hadley hadley removed the in progress label Jun 7, 2016

@lock lock bot locked and limited conversation to collaborators Sep 25, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.