R基础无法解析标准声明的时间格式。

huangapple go评论98阅读模式
英文:

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:

  1. base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")

returns:

  1. Error in charToDate(x) :
  2. 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

  1. locale:
  2. [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
  3. [3] LC_TIME=de_BE.UTF-8 LC_COLLATE=en_US.UTF-8
  4. [5] LC_MONETARY=de_BE.UTF-8 LC_MESSAGES=en_US.UTF-8
  5. [7] LC_PAPER=de_BE.UTF-8 LC_NAME=C
  6. [9] LC_ADDRESS=C LC_TELEPHONE=C
  7. [11] LC_MEASUREMENT=de_BE.UTF-8 LC_IDENTIFICATION=C
  8. attached base packages:
  9. [1] stats graphics grDevices utils datasets methods base
  10. loaded via a namespace (and not attached):
  11. [1] compiler_3.6.2

答案1

得分: 1

因为你的地区不同,月份的缩写也不同。

在我的英国地区:

  1. Sys.getlocale("LC_TIME")
  2. [1] "English_United Kingdom.1252"
  3. base::as.Date("07 Mar 2025", tryFormats = "%d %b %Y")
  4. [1] "2025-03-07"

在德语比利时地区:

  1. Sys.setlocale("LC_TIME","German_Belgium.1252")
  2. [1] "German_Belgium.1252"
  3. base::as.Date("07 Mar 2025", tryFormats = "%d %b %Y")
  4. Error in charToDate(x) :
  5. character string is not in a standard unambiguous format
  6. base::as.Date("07 Mrz 2025", tryFormats = "%d %b %Y")
  7. [1] "2025-03-07"
  8. base::as.Date("07 März 2025", tryFormats = "%d %b %Y")
  9. [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:

  1. Sys.getlocale("LC_TIME")
  2. [1] "English_United Kingdom.1252"
  3. base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")
  4. [1] "2025-03-07"

In a German-speaking Belgian locale:

  1. Sys.setlocale("LC_TIME","German_Belgium.1252")
  2. [1] "German_Belgium.1252"
  3. base::as.Date("07 Mar 2025", tryFormats ="%d %b %Y")
  4. Error in charToDate(x) :
  5. character string is not in a standard unambiguous format
  6. base::as.Date("07 Mrz 2025", tryFormats ="%d %b %Y")
  7. [1] "2025-03-07"
  8. base::as.Date("07 März 2025", tryFormats ="%d %b %Y")
  9. [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.

huangapple
  • 本文由 发表于 2020年1月6日 18:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定