Readr :: read_csv () does not read dates and returns NA

I have a csv file test.csv

with a column containing dates:

V1
14-01-02 9:10
14-01-02 9:10
14-01-02 9:21
14-01-02 9:34
14-01-02 9:34
14-01-02 9:34

      

Reading the file with readr::read_csv

gives NAs

:

V1
1 <NA>
2 <NA>
3 <NA>
4 <NA>
5 <NA>
6 <NA>
Warning message:
9 problems parsing 'test.csv'. See problems(...) for more details. 

      

read.csv

seems to be able to download it without issue, but it is too slow. The actual table is 322,509 x 45 and I would prefer not to list every column type with an option col_type

.

Is there anyway it can load the column as a character?

+3


source to share


2 answers


You can specify column types in a list, where you only specify columns where you don't want readr to try to recognize the column type.

read_csv("test.csv", col_types = list(V1 = col_datetime()))

      



See the readme on cran for details .

+5


source


From ?read_csv

he talks about the argument col_type

,

If "NULL", the column type will be imputed from the first 30 rows in the input. It's convenient (and fast), but not reliable. If the imputation fails, you need to supply the correct types.

It looks like you might be stuck with



read_csv("temp.csv", col_types="T")  # T for datetimes

      

You can also try reading the first line with read.csv

, get the classes, and then read the whole file with read_csv

. You will need to convert the character to datetime after the fact.

samp <- read.csv("test.csv", nrows=1, strings=F)               # read one row
cols <- sapply(samp, class)                                    # get classes
key <- c("character"="c", "integer"="i", "logical"="l")        # make key, etc.
read_csv("test.csv", col_types=paste(key[cols], collapse=""))  # read with read_csv

      

+1


source







All Articles