How to use “cols()” and “col_double” with respect to comma as decimal mark
up vote
2
down vote
favorite
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?
r csv readr
add a comment |
up vote
2
down vote
favorite
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?
r csv readr
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
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?
r csv readr
r csv readr
edited Apr 6 '17 at 0:19
AkselA
4,00121125
4,00121125
asked Apr 5 '17 at 15:03
Pelle
637
637
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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
Thanks, that works! I tried to set thelocale
within thecol_double()
call. Can you also tell me why I am getting theError: expecting a string
when usingcols(Date = col_date(format = "%d.%m.%Y"))
?
– Pelle
Apr 6 '17 at 8:16
Oh, got it already: it'scol_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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
Thanks, that works! I tried to set thelocale
within thecol_double()
call. Can you also tell me why I am getting theError: expecting a string
when usingcols(Date = col_date(format = "%d.%m.%Y"))
?
– Pelle
Apr 6 '17 at 8:16
Oh, got it already: it'scol_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
add a comment |
up vote
3
down vote
accepted
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
Thanks, that works! I tried to set thelocale
within thecol_double()
call. Can you also tell me why I am getting theError: expecting a string
when usingcols(Date = col_date(format = "%d.%m.%Y"))
?
– Pelle
Apr 6 '17 at 8:16
Oh, got it already: it'scol_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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
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
answered Apr 5 '17 at 15:07
MrFlick
118k11128158
118k11128158
Thanks, that works! I tried to set thelocale
within thecol_double()
call. Can you also tell me why I am getting theError: expecting a string
when usingcols(Date = col_date(format = "%d.%m.%Y"))
?
– Pelle
Apr 6 '17 at 8:16
Oh, got it already: it'scol_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
add a comment |
Thanks, that works! I tried to set thelocale
within thecol_double()
call. Can you also tell me why I am getting theError: expecting a string
when usingcols(Date = col_date(format = "%d.%m.%Y"))
?
– Pelle
Apr 6 '17 at 8:16
Oh, got it already: it'scol_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
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 6 '17 at 8:16
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 6 '17 at 8:16
Oh, got it already: it's
col_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
Oh, got it already: it's
col_types = cols(Date = col_date(format = "%d.%m.%Y"))
– Pelle
Apr 6 '17 at 8:24
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43234976%2fhow-to-use-cols-and-col-double-with-respect-to-comma-as-decimal-mark%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown