【问题标题】:Read European formatted csv with googlesheets package使用 googlesheets 包读取欧洲格式的 csv
【发布时间】:2018-03-07 11:02:28
【问题描述】:

我无法通过library(googlesheets) 包从谷歌表格中高效地导入格式化为欧式格式的文件(, 逗号表示小数,. 点表示数千个12.300,35)。 R 会自动将逗号读取为分隔符,而不是小数点。

下载工作表时(通过 R),它会自动选择逗号分隔符。我可以自定义吗?

这是我通过shared google sheets link 发送的数据。

我的代码:

library(googlesheets)

# Authenticate with your google sheets
sheets <- gs_ls()

# Import
spreadsheet <- gs_title("sample_data")

# Read dataset
sample <- gs_read(spreadsheet, ws = 1)

结果错误:

 jaren hbo_procent wo_procent
   <int>       <dbl>      <dbl>
 1  2006       66.0        9.00
 2  2007       67.0       97.0 
 3  2008        7.00     104   
 4  2009       73.0      112   
 5  2010       75.0      119   
 6  2011       77.0      128   
 7  2012       78.0      137   
 8  2013       76.0      142   
 9  2014       74.0      148   
10  2015       75.0      163   
11  2016       75.0      181   
12  2017       78.0      195   

【问题讨论】:

    标签: r csv import r-googlesheets


    【解决方案1】:

    googlesheets 有一个类似 readr 的接口,特别是它提供了 locale 参数。你可以在这里阅读更多信息:http://readr.tidyverse.org/articles/locales.html

    在这种情况下,我们需要指定小数点。

    我制作了自己的 OP 表格副本。我不确定原件是否正确发布到网络,这是通过 Sheets v3 API 使世界可读的必要条件。这(令人困惑地)不同于将工作表“在网络上公开”。

    library(googlesheets)
    
    ## I made a copy of OP's original Sheet
    spreadsheet <- gs_title("Copy of sample_data")
    #> Sheet successfully identified: "Copy of sample_data"
    
    # Read dataset
    sample <- gs_read(
      spreadsheet,
      ws = 1,
      locale = readr::locale(decimal_mark = ",")
    )
    #> Accessing worksheet titled 'Blad1'.
    #> Parsed with column specification:
    #> cols(
    #>   jaren = col_double(),
    #>   hbo_procent = col_double(),
    #>   wo_procent = col_double()
    #> )
    sample
    #> # A tibble: 12 x 3
    #>    jaren hbo_procent wo_procent
    #>    <dbl>       <dbl>      <dbl>
    #>  1 2006.        6.60       9.00
    #>  2 2007.        6.70       9.70
    #>  3 2008.        7.00      10.4 
    #>  4 2009.        7.30      11.2 
    #>  5 2010.        7.50      11.9 
    #>  6 2011.        7.70      12.8 
    #>  7 2012.        7.80      13.7 
    #>  8 2013.        7.60      14.2 
    #>  9 2014.        7.40      14.8 
    #> 10 2015.        7.50      16.3 
    #> 11 2016.        7.50      18.1 
    #> 12 2017.        7.80      19.5
    

    或者,要制作更容易让更多人“阅读”的工作表,您可以执行 文件 > 电子表格设置并选择,例如,美国作为工作表本身的区域设置.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2021-03-08
      • 2010-11-26
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多