8

I would like to parse my columns with the readr package to the right type while reading.

Difficulty: the fields are separated by semicolon (;), while comma (,) is used as decimal mark.

library(readr)

# Test data:
T <- "Date;Time;Var1;Var2
      01.01.2011;11:11;2,4;5,6
      02.01.2011;12:11;2,5;5,5
      03.01.2011;13:11;2,6;5,4
      04:01.2011;14:11;2,7;5,3"

read_delim(T, ";")
# A tibble: 4 × 4
#              Date     Time  Var1  Var2
#             <chr>   <time> <dbl> <dbl>
# 1       01.01.2011 11:11:00    24    56
# 2       02.01.2011 12:11:00    25    55
# 3       03.01.2011 13:11:00    26    54
# 4       04:01.2011 14:11:00    27    53

So, I thought the parsing thing would work like this, but I am always getting the error message:

read_delim(T, ";", cols(Date = col_date(format = "%d.%m.%Y")))
# Error: expecting a string

Same here:

read_delim(T, ";", cols(Var1 = col_double()))
# Error: expecting a string

I think I am doing somthing fundamentally wrong. ;)

Also I would appreciate a tip on how I can tell read_delim to understand the commas as decimal mark. read.delim can do it quite easily with dec = "," but I would really like to use the "readr"-Package from the beginning without struggling around. There was a col_euro_double function in the former version, but it has been removed. What are the alternatives now?

AkselA
  • 8,153
  • 2
  • 21
  • 34
Pelle
  • 257
  • 2
  • 8

1 Answers1

16

Specify the locale= when using read_delim()

read_delim(T, ";", locale=locale(decimal_mark = ","))
#               Date       Time  Var1  Var2
#              <chr>     <time> <dbl> <dbl>
# 1       01.01.2011 40260 secs   2.4   5.6
# 2       02.01.2011 43860 secs   2.5   5.5
# 3       03.01.2011 47460 secs   2.6   5.4
# 4       04:01.2011 51060 secs   2.7   5.3
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks, that works! I tried to set the `locale` within the `col_double()` call. Can you also tell me why I am getting the `Error: expecting a string` when using `cols(Date = col_date(format = "%d.%m.%Y"))` ? – Pelle Apr 06 '17 at 08:16
  • Oh, got it already: it's `col_types = cols(Date = col_date(format = "%d.%m.%Y"))` – Pelle Apr 06 '17 at 08:24