英文:
R base fails to parse standard declared time format
问题
base::as.Date("07 Mar 2025", tryFormats = "%d %b %Y") 返回: Error in charToDate(x) : 字符串不符合标准的明确格式
我使用的是 R 3.6.2,sessioninfo 返回:
矩阵乘积:默认
BLAS:/usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK:/usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
语言环境:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=de_BE.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=de_BE.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=de_BE.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=de_BE.UTF-8 LC_IDENTIFICATION=C
附加的基本包:
[1] stats graphics grDevices utils datasets methods base
通过命名空间加载(未附加):
[1] compiler_3.6.2
英文:
This:
base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")
returns:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm using R 3.6.2 and sessioninfo returns:
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=de_BE.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=de_BE.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=de_BE.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=de_BE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.2
答案1
得分: 1
因为你的地区不同,月份的缩写也不同。
在我的英国地区:
Sys.getlocale("LC_TIME")
[1] "English_United Kingdom.1252"
base::as.Date("07 Mar 2025", tryFormats = "%d %b %Y")
[1] "2025-03-07"
在德语比利时地区:
Sys.setlocale("LC_TIME","German_Belgium.1252")
[1] "German_Belgium.1252"
base::as.Date("07 Mar 2025", tryFormats = "%d %b %Y")
Error in charToDate(x) :
character string is not in a standard unambiguous format
base::as.Date("07 Mrz 2025", tryFormats = "%d %b %Y")
[1] "2025-03-07"
base::as.Date("07 März 2025", tryFormats = "%d %b %Y")
[1] "2025-03-07"
你可以尝试切换到英语地区使用 Sys.setlocale
解释数据。请注意,1252 是 Windows 特定的,如果在 Linux 中使用,你可能需要类似于 en_GB.UTF-8
这样的设置。
英文:
It is because of your locale, the month abbreviations are different.
In my UK locale:
Sys.getlocale("LC_TIME")
[1] "English_United Kingdom.1252"
base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")
[1] "2025-03-07"
In a German-speaking Belgian locale:
Sys.setlocale("LC_TIME","German_Belgium.1252")
[1] "German_Belgium.1252"
base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")
Error in charToDate(x) :
character string is not in a standard unambiguous format
base::as.Date("07 Mrz 2025", tryFormats ="%d %b %Y")
[1] "2025-03-07"
base::as.Date("07 März 2025", tryFormats ="%d %b %Y")
[1] "2025-03-07"
You could try switching to an English locale to interpret the data using Sys.setlocale
. Note that the 1252 ones are Windows-specific, you would probably need to use something like en_GB.UTF-8
in Linux.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论