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











up vote
2
down vote

favorite
1












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?










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    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?










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 6 '17 at 0:19









      AkselA

      4,00121125




      4,00121125










      asked Apr 5 '17 at 15:03









      Pelle

      637




      637
























          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





          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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





          share|improve this answer





















          • 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















          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





          share|improve this answer





















          • 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













          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 5 '17 at 15:07









          MrFlick

          118k11128158




          118k11128158












          • 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


















          • 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
















          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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()