Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

csv

readr

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?

like image 925
Pelle Avatar asked Apr 05 '17 15:04

Pelle


People also ask

How do you use a comma with decimals?

And in countries where a point is used as a decimal separator, a comma is usually used to separate thousands. So, for example, twelve thousand five hundred with a decimal of five zero is written differently depending on the country: In the USA, Mexico, or the UK, it would be written: 12 500.50 or 12,500.50.

How do I change commas to decimal points in Word?

Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.

Can you use a comma as a decimal in C++?

If you want to allow user to input comma as decimal separator, you will need to apply locale to input stream too: std::cin. imbue(std::locale( "se-SE" ));


1 Answers

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
like image 129
MrFlick Avatar answered Sep 30 '22 18:09

MrFlick