How to use “cols()” and “col_double” with respect to comma as decimal mark

萝らか妹 提交于 2019-12-30 07:18:08

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/43234976/how-to-use-cols-and-col-double-with-respect-to-comma-as-decimal-mark

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