R中的flipTime包中的AsDate函数出错

时间:2018-10-03 10:12:32

标签: r date

我打算将“ Jan 2009”字符串转换为日期。但是,我遇到以下给定的错误。

AsDate("Jun 2009")

Error in regexec(patt, x1, perl = TRUE, ignore.case = TRUE) : 
unused argument (perl = TRUE)

我的会话信息保持不变:

R version 3.2.1 (2015-06-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_IN       LC_NUMERIC=C         LC_TIME=en_IN       
 [4] LC_COLLATE=en_IN     LC_MONETARY=en_IN    LC_MESSAGES=en_IN   
 [7] LC_PAPER=en_IN       LC_NAME=C            LC_ADDRESS=C        
[10] LC_TELEPHONE=C       LC_MEASUREMENT=en_IN LC_IDENTIFICATION=C 

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

other attached packages:
[1] flipTime_2.8.1 zoo_1.7-13    

loaded via a namespace (and not attached):
[1] Rcpp_0.12.19     plotly_3.6.0     magrittr_1.5     munsell_0.5.0   
[5] colorspace_1.3-2 lattice_0.20-33  R6_2.2.2         rlang_0.2.2     
[9] stringr_1.3.1    httr_1.3.1       plyr_1.8.4       tools_3.2.1     
[13] grid_3.2.1       gtable_0.2.0     htmltools_0.3.6  lazyeval_0.2.1  
[17] digest_0.6.17    tibble_1.4.2     crayon_1.3.4       gridExtra_0.9.1 
[21] purrr_0.2.5      tidyr_0.8.1      ggplot2_2.2.1    viridis_0.3.4   
[25] base64enc_0.1-3  htmlwidgets_1.3  glue_1.3.0       memoise_1.0.0   
[29] stringi_1.2.4    compiler_3.2.1   pillar_1.3.0     scales_1.0.0    
[33] jsonlite_1.5     lubridate_1.3.3 

将感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码对我有用。或者,尝试以下操作:

date <- "Jun 2009"
date <- paste("1", date) # Default day value 
as.Date(date, format="%d %b %Y")

输出:

[1] "2009-06-01"

PS:如果字符串输入中缺少天,月或年之一,as.Date将给出NA作为输出,因此我们要添加默认的天值

希望这会有所帮助!