0

我正在尝试通过阅读此 googledoc googlesheets4

该文件包含 3 列:

  1. id, 作为字符
  2. start_date/ end_date:此列包含日期(格式为2020-01-31)和文本字符串(即字符串为in_stockin_use

我让任何知道链接的人都可以阅读这个 googlesheet 。

我读取数据帧如下:

suppressPackageStartupMessages({
  library(tidyverse)
  library(lubridate)
  library(readxl) 
  library(googlesheets4)})

df <- read_sheet('1rS_Zw4Qc8S4oV5-u_YZJACNBqu3VDQUMf9N_dGrLUo8',sheet = 'test_import')

虽然最初看起来不错,但当我检查它是如何导入的时,结果如下:

> head(df)
# A tibble: 6 x 3
     id start_date end_date 
  <dbl> <list>     <list>   
1     1 <chr [1]>  <chr [1]>
2     2 <chr [1]>  <chr [1]>
3     3 <chr [1]>  <chr [1]>
4     4 <chr [1]>  <chr [1]>
5     5 <chr [1]>  <chr [1]>
6     6 <chr [1]>  <chr [1]>

虽然 RStudio 在预览中正确显示了数据框,但列start_date作为列表end_date加载(这从未发生在我身上)。

当我尝试通过执行以下操作将列转换为日期时:

df %>%
  mutate(start_date = as.Date(start_date))

我得到错误:

Error in as.Date.default(start_date) : 
  do not know how to convert 'start_date' to class “Date”

如何在保留日期列的同时正确导入它start_dateend_date以便对它们执行操作?

谢谢您的帮助。

    > sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] googlesheets4_0.2.0.9000 readxl_1.3.1             lubridate_1.7.4          forcats_0.4.0            stringr_1.4.0            dplyr_0.8.3             
 [7] purrr_0.3.4              readr_1.3.1              tidyr_1.0.0              tibble_3.0.1             ggplot2_3.2.1            tidyverse_1.2.1         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        cellranger_1.1.0  pillar_1.4.4      compiler_3.6.1    tools_3.6.1       packrat_0.5.0     googledrive_1.0.1 jsonlite_1.6.1   
 [9] lifecycle_0.2.0   nlme_3.1-141      gtable_0.3.0      lattice_0.20-38   pkgconfig_2.0.3   rlang_0.4.6       cli_2.0.2         rstudioapi_0.10  
[17] haven_2.1.1       withr_2.2.0       xml2_1.2.2        httr_1.4.1        generics_0.0.2    vctrs_0.3.1       hms_0.5.1         grid_3.6.1       
[25] tidyselect_0.2.5  glue_1.4.1        R6_2.4.1          fansi_0.4.1       modelr_0.1.5      magrittr_1.5      backports_1.1.4   scales_1.0.0     
[33] ellipsis_0.3.1    rvest_0.3.4       assertthat_0.2.1  colorspace_1.4-1  stringi_1.4.6     lazyeval_0.2.2    munsell_0.5.0     broom_0.5.2      
[41] crayon_1.3.4  
4

1 回答 1

0

尝试这样做(dplyr 1.0.0)。您有文本in_stockin_use日期列。因此,出现错误。

library(tidyverse)
df <- read_sheet('1rS_Zw4Qc8S4oV5-u_YZJACNBqu3VDQUMf9N_dGrLUo8',sheet = 'test_import', col_types = "c")
df %>% 
  mutate(across(ends_with("_date"), ymd)) 

或旧的 dplyr

df %>% 
  mutate_at(vars(ends_with("_date")), ymd)
于 2020-06-23T15:35:31.597 回答